The Java runtime memory is divided into five parts:
Thread sharing: Heap memory, method area (including Chang)
Thread private: Stack memory, local method stack, program counter
String is not a basic data type, what is the memory location of a string? A total of two cases:
1, String Direct assignment:
String s = "haha";
The reference to S is present in the stack memory, and the reference point to the haha exists in the constant pool of the method area (first to determine if there is a haha in the constant pool, which is directly pointing to the existence)
2. String Object New Creation
string s = new string ("haha");
The reference to S is present in the stack memory, referencing the Haha object that exists in heap memory (every new time, creating a new Haha object in the heap)
Summarize:
1, a string type of reference, are present in the stack memory;
2, string reference point, the direct assignment exists stack memory, new out of the existing stack memory.
About the memory location of string in Java