Maybe everyone is familiar with Java arrays. Recently I encountered a problem about Java Array Memory allocation.
Haha. Suddenly I found that the statement "the basic data type is stored in the stack memory, and the object is stored in the heap memory" in many books is totally wrong. The following is a simple example code:
Copy codeThe Code is as follows: public class Test {
Public static void main (String [] argv ){
// Static initialization Array
String [] names = {"Michael", "Orson", "Andrew "};
// Dynamically initialize the Array
String [] animal = new String [4];
// Point animal to the array referenced by the namens Array
Names = animal;
System. out. println (names. length );
System. out. println (animal. length );
}
}
"The size of the Java array cannot be changed." You may have heard of it. The above code has a problem. The length of animal [] is 4, the length of the names [] array is only 3, but after a value assignment statement, the size of both arrays is changed to 4. Does this change the array size? The problem is blocked! Well, ask the technical predecessors to have a new understanding of the array storage method. The following is my understanding: (if something is wrong, you will be able to point it out .)
The above names and animal do not represent this array object, but only the array variables. They are the same as pointers in C. Such variables are called reference variables. The array object is stored in the heap memory, and the size cannot be changed, but the array variable can point to other array objects. You can see the figure below:
The blue dotted line is the value assignment statement names = animal; the array objects in the heap memory pointed to by the names and animal array variables; the red line is the value assignment statement names = animal; after that, both names and animal array variables point to an array object at the same time. Of course, at this time, the Java garbage collection mechanism will find the unreferenced array object and then take it away. We can also see that "Michael", "Orson", and "Andrew" are basic data types. But they are stored in the heap memory. In fact, it should be said that local variables are placed in the stack memory (such as the referenced type variables like names [], animal [], and some basic types of variables ), however, the objects referenced by the Application variables are stored in heap memory. (Including arrays and some common class objects we usually write) Java objects in the heap memory usually do not allow direct access, but you can think of the consequences of direct access. To access the objects in the heap memory, you need to reference the variable mediation. When is the variable stored in the stack memory just a reference variable? When has it changed its identity to a genuine JAVA object? Well, let's look at the example below:Copy codeThe Code is as follows: public class Animal {
Private String name;
Private int age; Animal (String name, int age ){
This. name = name;
This. age = age;
} Public void info (){
System. out. println (name + "" + age );
}
}
Public class Test {public static void main (String [] argv ){
// Dynamically initialize the Array
Animal [] animal = new Animal [2];
Animal cat = new Animal ("cat", 1 );
Animal dog = new Animal ("dog", 2 );
Animal [0] = dog;
Animal [1] = cat; // when the array variable references the object's method (or attribute), it becomes the actual Java object
System. out. println (animal. length );
// The dog object originally stored in the stack memory is referenced and changed to the actual object by calling the object method.
Dog.info ();
Animal [0]. info ();
}
}
Only when the reference variable in the stack memory calls the object method or points to the object's attributes, it truly becomes an object from the variable. (For example, in the example above, cat, dog object references the variable, animal [] array variable ). Through animal [0] = dog;
Animal [1] = cat; so that both variables point to the objects stored in the heap memory, so the printed information is the same.
The Blue Line in is a value assignment statement: animal [0] = dog;
Animal [1] = cat; previous variables point to the State. The red dotted line is the State after the value assignment statement, animal [0] And dog, animal [1] and cat all point to the same heap memory space.