In today's post we will go through various ways of creating strings and understanding memory management of strings.
Java provides two ways of creating strings: using string literal or with new operator. So next question comes in mind is: why there are two different ways and what is the advantage of the same.
Before we go into more details, let's first understand internal memory management of String objects.
JVM maintains string pool to maintain all string variaobles in the memory. String pools are generally stored in the PermGen area of Heap memory since they are constants and will be required for longer period. So when you create string variable using literals, jvm first checks if variable already exists in the string pool. If it does then only reference of the object will be assigned to new string variable whereas when strings are created using new operator they are treated as any other java objects and will be stored in the heap memory.
So let's understand this concepts using below example,
static String iAmReference = "i am reference";
Java provides two ways of creating strings: using string literal or with new operator. So next question comes in mind is: why there are two different ways and what is the advantage of the same.
Before we go into more details, let's first understand internal memory management of String objects.
JVM maintains string pool to maintain all string variaobles in the memory. String pools are generally stored in the PermGen area of Heap memory since they are constants and will be required for longer period. So when you create string variable using literals, jvm first checks if variable already exists in the string pool. If it does then only reference of the object will be assigned to new string variable whereas when strings are created using new operator they are treated as any other java objects and will be stored in the heap memory.
So let's understand this concepts using below example,
static String iAmReference = "i am reference";
static String iAmSameReference = "i am reference";
Here, first variable, iAmReference will be created as new object since string doesn't exist in the string pool yet. Now, when we try to create iAmSameReference , jvm checks string pool and finds there is one object already created (iAmReference) and so will assign the reference of the same object to iAmSameReference.
if(iAmReference == iAmSameReference){
System.out.println("References are always same");
}
Output: References are always same
Now, lets see what happens in case of new operator.
static String iAmNewObject1 = new String("i am reference");
static String iAmNewObject2 = new String("i am reference");
Here, both objects are created in the heap memory, no different than any other objects. To verify what we just discussed, try to execute below statements.
//compare reference with object.
if(!(iAmReference == iAmNewObject1)){
System.out.println("Objects and reference are never same");
}
//compare objects created in heap memory.
if(!(iAmNewObject1 == iAmNewObject2)){
System.out.println("Two objects are never same in java. Remember hash code ??");
}
Output:
Objects and reference are never same.
Two objects are never same in java. Remember hash code ??
Now, what if you want both objects, iAmNewObject1 and iAmNewObject2, to put in string pool ?
Java provides method called intern() which provides this facility. So now let's put both of these objects in the string pool to save memory and avoid duplication of objects.
iAmNewObject1 = iAmNewObject1.intern();
iAmNewObject2 = iAmNewObject2.intern();
You can verify if objects are now created in the string pool or not by executing below statements:
if(iAmReference == iAmNewObject1){
System.out.println("Now iAmNewObject1 is put on String pool from heap memory.");
}
if(iAmNewObject1 == iAmNewObject2){
System.out.println("Now iAmNewObject2 is also put on String pool from heap memory.");
}
Output:
Now iAmNewObject1 is put on String pool from heap memory.
Now iAmNewObject2 is also put on String pool from heap memory.
Hope, this will help you clear your doubts about java string handling.
Working Code can be found here.
Working Code can be found here.
No comments:
Post a Comment