With Java version 1.6 the output is false true
, but with version 1.8 the output changed to true true
.
Can some one explain why is this happening?
Intern method is used to refer the corresponding string constant pool of created objects in the heap, and if the object is not there then it will create a String constant pool. Please correct me if my understanding is wrong.
public class Intern_String2 {
public static void main(String[] args) {
String s1 = new String("durga"); //object created in heap
String s2 = s1.concat("software");
//object durga software created in heap at runtime
String s3 = s2.intern();
// create durga software object in string constant pool as none exist.
System.out.println(s2==s3);//should be false but print true in 1.8 version.
String s4 = "durgasoftware";
System.out.println(s3==s4);//prints true in both version..
}
}