1.java Variable Storage domain
The storage area for Java variables is mainly in the following places:
(1) Register: Can be said to be the fastest storage area, in C + + can declare register variables, but in Java cannot declare register variables, but the compiler is determined at compile time.
(2) Stack: Holds references to basic types of data and objects, but the object itself is not in the stack, (through new) objects are placed in the heap or in a constant pool (the constant object of the string is placed in the constant pool).
(3) Heap: Used to store new objects.
(4) Constant pool: constant that holds string constants and basic type data (through public static final decoration).
(5) Static domain: Used to hold static members (statics).
(6) Non-RAM storage: such as hard disk.
The main point here is heap, stack and constant pool. The size and life cycle of the data stored in the stack is determined, and when the referenced data disappears, the stack data is reclaimed by the JVM. The data stored in the heap is uncertain, and when certain conditions are met, it is recycled by the JVM's garbage collection mechanism.
2. Example
such as the following code:
1 string S1 = "Example"; 2 string s2 = "Example"; 3 string s3 = new String ("example"); 4 string s4 = new String ("example");
For S1, S2 will be stored in the constant pool, for S3 (new out of the object), will first go to the constant pool to find there is no "example" object, if any, will create a "example" copy object in the heap, if not, you will first create a "example" in the constant pool Object, and then create a copy object in the heap.
So the question: string str = new String ("abc") creates several objects?
Answer Yue: one or two.
Java variable Storage