Home > Back-end >  The intern () to my whole again
The intern () to my whole again

Time:11-10

1 String str2=new String (" ABC ") + new String (" 01 ");
2 str2. Intern ();
3 strings str1="abc01";
4 System. Out. Println (str2==str1);

Today suddenly received and this is what is true.
The first step on a pile of str2 object abc01
The second step in the pool in a abc01 str2 constant
The third step str1 reference constant pool abc01
The results should not be the heap pool of==false? ! Zha return is true,,
Great god teach,

CodePudding user response:

Intern method of documentation has been explained very clearly, in the pool when there is no object, the object in the pool, and returns the object reference
So
Str2. Intern abc01 was put into the pool, and returns the str2 (reference) for str2 is abc01
So the second step is to understand a problem
Pool into abc01, str2 is to point to in the pool abc01 object
So str1 and str2 abc01 object in all point to the pool, so==is true

CodePudding user response:

reference 1st floor qybao response:
documentation of intern method have been explained very clearly, in the pool when there is no object, the object in the pool, and returns the object reference
So
Str2. Intern abc01 was put into the pool, and returns the str2 (reference) for str2 is abc01
So the second step is to understand a problem
Pool into abc01, str2 is to point to in the pool abc01 object
So str1 and str2 point to abc01 object in the pool, so==is true

Big thank you for your reply! So I always don't understand, "str2. Intern ();" After this step, str2 does not refer to the original on the heap that abc01, but in the language of the constant pool abc01? Or str2 and abc01 itself has become, in the constant pool,

CodePudding user response:

Str2 point to the constant pool object, heap object is moved to the constant pool, reference is not followed, the reference does not become pointed to the empty? Str2 itself in the stack, it will not become a constant pool of things,

CodePudding user response:

reference qybao reply: 3/f
str2 point to the object of the constant pool, heap object is moved to the constant pool, reference is not followed, the reference does not become pointed to the empty? Str2 itself in the stack, it will not become a constant pool,

Know that str2. Intern (), heap object is moved to the constant pool, so str2 referenced objects from the heap object into the constant pool abc01,
Thank you so much for bosses.
  • Related