Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why String intern method? [duplicate]

Tags:

java

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?

like image 545
Sachin Gorade Avatar asked Oct 15 '13 05:10

Sachin Gorade


People also ask

What is String intern () When and why should it be used?

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.

What is the use of the intern () method A It returns the existing String from memory B it creates a new String in the database C it modifies the existing String in the database?

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.

Can we have duplicate values in String constant pool?

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.

What is the use of String constant pool?

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.


2 Answers

The basic algorithm for .intern() is the following:

  1. Create a hash set of Strings
  2. Check to see if the String you're dealing with is already in the set
  3. If so, return the one from the set
  4. Otherwise, add this string to the set and return it

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.

like image 194
pnathan Avatar answered Oct 12 '22 01:10

pnathan


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

like image 27
Gyanendra Dwivedi Avatar answered Oct 12 '22 00:10

Gyanendra Dwivedi