Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

Monday, 22 December 2014

Difference between String Literal and String Object in Java


This is most confusing topic in java and its not given clearly in many websites what is the exactly difference between String Literal and String Object.And its a very basic question asked in interviews.
So here is my explanation on this:-

String are nothing but a sequence of characters for ex:"Hello".In java programming language string are objects.

The JVM maintains a special part of memory called "String Literal pool" or "String constant pool" where all string gets stored.This is done to avoid unnecessary creation of same object again and again which helps in saving memory as well as time.

When you declare a String as

String strName1="Kavita";

Here JVM first checks whether a String named "Kavita" already present in String pool.If yes,variable 'strName' refers to the object "Kavita". If no,then it creates an object "Kavita" in String pool and variable 'strName' points to this object.
This declaration creates ONE object.

Similarly, when you create another object as

String strName2="Kavita";

In this case , JVM checks the string "Kavita" in String pool.As "Kavita" object is already present in String pool. JVM returns the reference of "Kavita" object to variable 'strName2'.So NO objects gets created.

And both variable 'strName1' and 'strName2' points to the same object "Kavita" present in string pool.This way JVM save the memory and time in creation of string objects.If you call '==' operator on both the variable,then it will return TRUE.

String objects gets stored in heap area of memory.String pool is located in premgen area of heap in Java version 1.6 or before but in Java 1.7 , it is moved to heap area of memory.

When you declare String using new operator as,

String strName3=new String("Rob");

Here ,'new String("Rob")' will always a create an object in heap and another object "Rob" will be created which gets stored in String pool.The reference of new String("Rob") will be returned to variable strName3. So in this case,Two objects gets created.


Happy Learning. :)

Thursday, 25 July 2013

What is the differnce between String and StringBuffer class?

The basic differnece between String and StringBuffer class is String is immutable(means value stored in string object cannot be changed) where as StringBuffer is not.

Whenever you do any changes to your String object,it creates a new string object and does not change the existing object.

For ex:

So suppose you declare a String object:

 String test= “Hello”;  

Next, you want to append “Guest” to the same String. What do you do?


 test=test+"Guest";  


When you print the contents of test the output will be “Hello Guest”. Although we made use of the same object(test), internally a new object was created in the process. So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.This creates a performance issue.

In order to avoid this performance issue ,we use StringBuffer/StringBuilder class to efficiently use append and other string operations.