Search This Blog

Wednesday, February 15, 2012

String In Java

If we talk about the Strings in Java ,its very interesting topic and really important .

Lets discuss how to create String object in Java.

There are two ways for this.
1. by using a String Literal and
2. by using a new Keyword.

First discuss about using String Literal-
If we create an String object like
String s="khemraj";
In this case only one String object will create in 'String Constant pool' and reference of that newly created object "khemraj" will be given to
reference variable s.

if you again create a String object using the same String Literal "khemraj" then the jvm will check the 'String constant pool' that Is there any existing object with the same literal?
if it already exist then it will not create any new object and will provide the same reference to this also. otherwise it will create a new object.

Now discuss creating String object Using new Keyword -
If you create a String object like this-
String s1=new String("khemraj");

In this case two objects will be created.
one in a 'String Constant Pool' and another in the Heap Memory. and s1 will contain the reference of object created in Heap Memory.

what will happen if you again create String object using the same -
String s2=new String("khemraj");

In this case only one object will be creat and that will be in Heap and reference of this newly created object in Heap, will be provide to s2. Not any object will be create in 'String Constant Pool' because there is already an object present with the same value "khemraj" .

Thats it.

Sunday, January 31, 2010

polymorphism in java

let,s talk something about polymorphism. all of we know that generally "poly" means "many".
so polymorphism means "there is only one thing and it has many forms".

Lets understand it from a simple example:
Suppose there is a company. In that company there will be many departments .
In each department there will be a manager ie;account department will have Account manager,Hr department will have HR manager etc etc...

Now suppose the Boss of the company wants to arrange a meeting for all the managers,
then he will simply say that call all the managers then managers of all departments will come for meeting.
there is no need to call everyone individually.every manager will come in the meeting.

The main Use of Polymorphism is "Reduce the complexity"

There are two types of polymorphism.
1. Compile time Polymorphism
2.RunTime Polymorphism

Compiletime Polymorphism:
In compile time polymorphism object is bounded with its functions
at compile time.
Now there arises a question that object is created at run time not at compile time.
So here it is assumed that when the object will create at run time then which functions will run.
Run Time Polymorphism:
In this, object is bounded with its functions at run time.

Compiletime Polymorphism is achieved by two ways.

first one operator Overloading
second is function Overloading

java does not support Operator Overloading.

Runtime Polymorphism is achieved by Function Overriding

Futher we will talk later.

Khemraj Singh
Sun Certified Java Programmer