Tuesday 17 January 2017

Intern method in String pool

Intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be added in String pool and the reference of this String is returned.

interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String().
String s1 = "Rakesh";
String s2 = new String("Rakesh").intern();


s1==s2 return true in above case now.


No comments:

Post a Comment