First, understand a few concepts:
Stacks: A reference to a variable data and an object that holds the base type, but the object itself is not stored on the stack, but is stored in the heap (the new object) or in a constant pool (the string constant object is stored in a constant pool.) )
When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically releases the memory space allocated for the variable, which can be used as an immediate alternative.
Heap: Stores all new objects. The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine.
Chang: A storage area allocated in the heap that holds string constants and basic type constants (public static final)
For strings: The references to their objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool and stored in the heap if the runtime (new) is established. For equals equal strings, there is always only one copy in the constant pool, with multiple copies in the heap.
The variable in the stack holds the reference, which is an address. This judgment of "STR1==STR2" is essentially the two-string address of the comparison. Therefore, if you are initializing with quotation mark initialization (String str1= "abc"), then two str is equal if the given position is the same. And the object that is produced by the new method. is to be reborn into a storage area in the heap, at which point the object in the stack holds the address name of the new zone.
What needs to be understood is that string itself is not allowed to be modified, and the reason we can reassign a string is essentially to change the reference of the object in the stack, and let him point back to the address of a new content.