-1
String s1=new String("Raam");
String s2=s1.concat("Kumar");
String s3=s2.intern();
String s4="RaamKumar";
System.out.println("s3==s4..."+(s3==s4)); //true

intern() method checks whether particular object exiting in String Constant pool or not . Oterwise it will create that object in String Constant pool. But What is the need of String intern().which scenario it is useful?

Alexis C.
  • 91,686
  • 21
  • 171
  • 177

1 Answers1

0

It is almost never useful. As per String class documentation, String literals are always interned. That is, if I say

String a = "A String";
String b = "A String";

, a single object will be put on the heap and both a and b will point to the same reference.

So, I would say this is syntactic sugar that makes any call to intern() unneeded.

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36