Here i will discuss some of the important yet confusing details of String.
String Storage: As you must be knowing, everything in java except primitives are Object. This is true for String too.
String is also an Object, hence it will reside on Heap only. But there is one more term used for String storage and that is String Pool/ String Literal Pool. Most of the time people think of it as a separate pool of String Object(True for me too ). But this is not true. String Pool/ String Literal Pool are nothing but a collection of references to String objects. As we know that String object is immutable it’s good to “share” the same String object with multiple references. (Vice versa is true also as String objects are shared between different references that is the reason String Objects are made Immutable. )
String Literal & Object: So when we are saying String literal we are actually referring the reference in String Pool and when we are saying String Object we are directly refering the String Object in Heap. But in both cases we are referring the Object in Heap only(Directly/ Indirectly).
How to Create: Now question arises how to create String Literal and String Object. As we know that we can create String Object in 2 ways.
- String str=new String(“abc”);
- String str1=”abc”;
When we are using new operator to create String, we are actually creating the String object in Heap like in First option. Here we are creating a String Object in Heap memory with value as “abc”. and a reference named “str”. Also note that as we are using “”(literals ) for creating thisString Object a literal will also be created. In total there will be 1 String Object 1 String literal and reference will be created. But reference will refer only the String object in Heap and not the literal in String pool.
And when we are using “” we are creating the literals(Everything inside “” are treated as literals). In this case(2nd scenario) JVM will look for all the references in String Pool and check if any of them pointing to String object in heap with value “abc“, if it finds any, it will return the reference of that object. Otherwise JVM will create a new String Object in Heap and interned it (Using inter() method) in String Pool(Reference for this object will be added in String Pool) for later refernce. And reference of the newly created object will be returned.
Now if we again write the same code to create String literal.
This time JVM look in String Pool to search for any object having same value to abc as ther is already one in String Pool JVM will just return the reference of existing String Object with value “abc”(Which was created in Second line) and no new String object will be created in Heap.
Garbage Collection for String: You might have heard that String literals are never eligible for Garbage Collection. Note we have mentioned String literal and not String Object. There is a difference. An object is eligible for garbage collection when it is no longer referenced from an active part of the application. String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection. But there may be some String Object which don’t have any reference then they will be eligible for garbage collection.
No related posts.