In Java version 1.6, the output is false true , but in version 1.8, the output changes to true true .
Can anyone explain why this is happening?
The Intern method is used to refer to the corresponding pool of string constants of created objects on the heap, and if the object is not there, it will create a pool of String constants. 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.. } }
java string
kumar anny
source share