This article stems from a colleague who asked me,
String str1 = "abc";
String str2 = "abc";
String str3 = new String ("abc");
STR1 = = str2 True, does it mean that str1 and str2 are allocated on top of the stack? They are not being new space.
Then LZ himself yy a bit, think of a way to use eclipse to see the stack allocation of variables, authoritative to be verified, if there is inappropriate, please enlighten!
The test procedure is as follows:
public class Stackstringtest {public static void main (string[] args) {String str1 = "abc"; String str2 = "abc"; System.out.println (str1 = = str2); String str3 = new String ("abc"); String STR4 = new String ("abc"); System.out.println (STR3 = = STR4); int a = 1;integer B = new Integer (1); System.out.println (a); System.out.println (b);}}
Put a breakpoint on each sentence, debug one-step operation, you can get the following results:
This leads to the conclusion that:
1. Whether string a = "a", or string a = new string ("a"), they are all assigned to the heap above (both have IDs, and an int type of a does not have an ID)
2. str1 = = Str2 is true because the system detects the existence of this address space (id=19) where value is "ABC" when Executing str2 = "abc", and in order to save space, it will no longer re-add a new area for str2. Instead, it points directly to the str1 heap interval, so the IDs of str1 and str2 are equal.
View stack allocations with eclipse