Thursday, 30 March 2017

Out of While and do-while , which is more efficient?


We often think that all the loops are same because we can use them almost everywhere and wherever we like in the Program but to become an efficient programmer we need to think where to use which loop so that our Programming can become efficient(More useful).

Explanation: 

We know the while loop first test the condition then executes the Statements, and do-while first executes the statement then checks the test condition. So any Programmer would like to have control on it's program from the very start, so the while loop is more efficient.

Another way of thinking this would be using the do-while loop because it will run at-least on time to show the first statement(Even when the condition is wrong), So user will get atleast some Output. While on the other hand the while loop will not show anything.

Wednesday, 29 March 2017

If Strings are immutable then what the other way to work on "strings"?


So we have studied that Strings are immutable so we have got StringBuffer class which represents strings in such a way that we can manipulate data in it(I am talking about Object).

Creating a StringBuffer reference object is same like creating any new Object of any class.

Example:
StringBuffer stringBuf = new StringBuffer("abc");

or 

StringBuffer stringBuf = new StringBuffer();
stringBuf.append("abc");

Explanation:
This will create an object "abc" in String Constant Pool,  and the reference variable will be stringBuf.

Monday, 27 March 2017

Creating a Simple Server- Client Connection in JAVA


The Client can connect to the Server with the same Socket by which the Server is Connected. Now the Server and Client can Communicate by reading and writing from the Socket. Communication is done by TCP(Transmission Control Protocol).

Example:

impot java.io.*;
//First the Server side

class ServerSide{
public static void main (String args[])
    {
       ServerSocket serverSocket = new ServerSocket(Port_number);
       System.out.println("SERVER STARTED");
       Socket s = ss.accept();
       System.out.println("CLIENT CONNECTED");
    }
}

//Now the Client Side

import java.net.*;

class Client {

public static void main(Sring args[])
    {
      Socket s= new Socket("localhost", Port_number);
      //Port no.should be same as the Server Port Number.
    }
 
}

Explanation:
So, the Client will connect to the Server Only when the Client class will execute. And One more thing make different java files for both. One for Client and one for Server.

Wednesday, 22 March 2017


How Strings are Immutable?

Let's Understand this with an Example.

Example:

String s1="abc";
String s2="abc";

s1="bcd";

System.out.print(s1);
System.out.print(s2);

/*Both Strings were referring to same object so if the reference variable
is changed then it should also change the String "s2". So let's take a look at the Output. */

Output:
bcd
abc


Explanation
So from the Above Example can we say that String are not Immutable? Well on the Outside it looks like the String is actually changed. But on the Inside, A Object is being Created(for "bcd") and s1 is assigned to it. Now both s1 and s2 are referring to different objects.

Wednesday, 15 March 2017

Now Time to Answer my First  Challenge Question. So I asked how many objects were created? 

Code: 
//Consider that a class is already created. 

String s = new String("abc");
String s1="abc";
String s2= new String("abc");

Now the Answer:
Total Objects will be 3.

Now the reason, How?
A. So First line of code has "new String". This will create an Object in the Heap Memory. But then the JVM will realise that it is a String, so it will create another Object in String Constant Pool and then the Garbage Collector will remove the Object that was created in Heap and "s" will be assigned to String Constant Pool Object. 

Now the second Statement.
It will refer to the same Object like "s" string. 

Now the third Statement.
It will again create a new Object of "s2" in Heap. But then it will create another object in String Constant Pool(But the Object "abc" is already created so it will also refer to it and no new Object will be Created in String Constant Pool). and then Garbage Collector will remove the Object present in Heap. 

Now You Know the Solution. Q& A's are All Welcomed.

Tuesday, 14 March 2017

Solve(JAVA CODE)

Code:
//Consider that a class is already created.
String name = null;
String s="";

How Many Objects are Created in the Above Code, and if object is created then what will be the Length of the Object Created?

Let's see who can answer this One.(JAVA CODE).


Code: 
//Consider that a class is already created. 

String s = new String("abc");
String s1="abc";
String s2= new String("abc");

How Many Objects are created in the above program(Total number of Objects)? Comment your Answer Below.