Concept of the concept of a boarding method with a change in release with a different version

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.. } } 
+7
java string
source share
3 answers

String.intern() returns the canonical instance of String. But this allows you to return the string passed to intern() (for example, the receiver / object of the call you are calling the method on) - this can happen if String is not in the internal table yet - this is a canonical instance now, Similarly, if this the string was already in the internal table String, intern() will return it.

 String s2 = "web".concat("sarvar"); String s3 = s2.intern(); System.out.println(s2 == s3); // prints "true" String s4 = "web".concat("sarvar"); String s5 = s4.intern(); System.out.println(s4 == s5); // prints "false" 
+2
source share

I would say that this happens in JAVA6 because the string pool was implemented using PermGen ... later, in JAVA7, String.intern() starts using HEAP memory ...

See the link for more details.

+1
source share

Jls indicates that it will become part of the constant pool. String literals and materials obtained by String.intern ().

There are no real specifications when they become part of this (first use or load of a class that defines a literal). He also does not indicate that he is not included in it, and what else can be interned.

So, based on your experiment, I think they changed the part when the Strings became part of the constant pool. Basically changed it from loading the class to first use. Thus, String.intern () can return "this", still adding it to the constant pool, becoming the same instance with the literal as at the beginning.

0
source share

All Articles