12

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?

Community
  • 1
  • 1
Sachin Gorade
  • 1,427
  • 12
  • 32

3 Answers3

12

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.

pnathan
  • 713
  • 3
  • 9
1

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

Community
  • 1
  • 1
Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
0

This method returns a canonical representation for the string object. It follows that for any two string a and t, s.intern()==t.intern() is true if and only if s.equal (t) is true.

Here is your Syntax:--

public String intern ()
Baby Groot
  • 4,637
  • 39
  • 52
  • 71