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.

Saturday, 11 February 2017

INHERITANCE  


Inheritance is basically nothing but property of One Class to inherit properties from another Class.
So, the question arises that how we can do that?
The answer is simple that when you define a Class(i.e. when you make a Class) and then you create another class which can also can access some parts of the Base class then it is considered as Inheritance.

Syntax: 
class base class-name{
};

class derived class-name : visibility mode base class-name{


}
Visibility mode can be private, protected or even public. it means which part the derived class can access from the Base Class(i.e. either private or protected or public).

Let's take an Example to understand it better.
Example: 

class Base{

//Let's assume that all functions are defined
} ;

class Subclass : public Base
{
};

Now the Subclass will be able to access all the Public members of the Class Base(Either Variables or Functions). If it was protected then the Sub class would be able to access the protected members of Class base.

I will post the next topic soon.If you have any questions you can ask in the Comments.
Thanks.