Strings with no corresponding objects in string constant pool while using intern method is returning the reference of the same object present in the heap. Isn't supposed to return the reference of an entirely different object which has been newly created by the intern method inside the String constant pool? Consider the given piece of code.
class Test{
public static void main(String[] args) {
String s1 = new String("durga");
String s2 = s1.concat("software");
String s3 = s2.intern();
System.out.println(s2 == s3);
String s4 = "durgasoftware";
System.out.println(s3 == s4);
}
}
input: deep (master *) LanguagePackageInJava $ javac Lecture14.java
deep (master *) LanguagePackageInJava $ java Test
output: true
true
How is the output of the first print statement true? According to me s2 is the object of the heap area while s3 should be the object of the string constant pool area. How can both of them point to the same object?