As explained in this When should we use intern method of String on String constants post that String literals are automatically pooled but for object constructed using new are not, so for that intern method is used. But even if we use intern method a new object will be created, then what is the use of intern method?
String s = "Example";
String s1 = new String("Example"); // will create new object
String s2 = new String("Example").intern(); // this will create new object
// but as we are calling intern we will get reference of pooled string "Example"
now
System.out.println(s == s1); // will return false
System.out.println(s == s2); // will return true
System.out.println(s1 == s2); // will return false
So what's the use of intern method?
Edit
I understood the working of intern method but my question is why intern method is there? because to call intern method we must create string object using new, which will create new instance of string!
String s3 = new String("Example"); // again new object
String s4 = s3.intern();
System.out.println(s3 == s4); // will return false
So calling intern method will not point s3 to pooled string. intern method will return reference to pooled string.
Also calling intern will push the string into pool if it is not already pooled? So does that mean every time I call intern on any string will be pushed to pool?
String Interning is a method of storing only one copy of each distinct String Value, which must be immutable. By applying String. intern() on a couple of strings will ensure that all strings having the same contents share the same memory.
The intern() method creates an exact copy of a string that is present in the heap memory and stores it in the String constant pool if not already present. If the string is already present, it returns the reference. The intern() method helps to save memory space and reuse it efficiently at the cost of time.
Although both reference variable s3 and s4 point to the String objects with the same value Hello but as these objects are creating with the new keyword, hence, these objects are stored in normal heap memory where the String objects with duplicate values can be stored.
The String constant pool is a special memory area. When we declare a String literal, the JVM creates the object in the pool and stores its reference on the stack. Before creating each String object in memory, the JVM performs some steps to decrease the memory overhead.
The basic algorithm for .intern()
is the following:
So it basically used to find the given string exist into the pool if it exist then it will get the same instance for that otherwise it creates the new instance for the new String.
Here is the sequence of events:
String s = "Example";
Create a Sting literal in pool
String s1 = new String("Example");
// will create new object <-- Correct, just create a new object
String s2 = new String("Example").intern();
//
Create the object only when String literal 'Example' is not found in the pool. In this case s1 will be retuned.
I hope you see here that intern is actually giving you an option to use String from the pool. And further in Java all the Strings are Object only; so the pool is actually reference of String having exact char sequence.
I remember a very good thread on stackoverflow itself; just found it for you .. Just check this one, it is awesome Is String Literal Pool a collection of references to the String Object, Or a collection of Objects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With