Probably Java arrays are familiar to me, and I recently encountered a problem with the memory allocation of Java arrays.
Oh. All of a sudden, I found a lot of books. The phrase "basic data types are stored in stack memory and objects are saved in heap memory" is completely wrong. Here's a simple example code:
Copy Code code as follows:
public class Test {
public static void Main (string[] argv) {
Statically initializing an array
String[] names = {"Michael", "Orson", "Andrew"};
Dynamic initialization of an array
string[] animal = new STRING[4];
Let animal point to the array referenced by the Namens array
names = animal;
System.out.println (names.length);
System.out.println (animal.length);
}
}
"Java array size cannot be changed"This may have been heard, and the code has a problem, animal [] length is 4, and names [] array length is only 3, but after an assignment statement, the size of two arrays becomes 4. Doesn't this change the size of the array? The problem is in front of you! Well, ask the tech seniors, so there's a whole new understanding of how the arrays are stored. Here is a bit of my understanding: (if there is a mistake, just be seen by the great God, you can also point out.) )
The above names and animal do not represent this array object, but only the variables of the array, and the pointer in C is the same, such variables are called reference variables. Array objects are stored in heap memory, the size of course can not be changed, but the array variable can point to other array objects, you can look at the following diagram:
The blue dotted line is the assignment statement names = animal; The array object in the heap memory that the names and animal array variables point to, and the red line is the assignment statement names = animal, after which both the names and the animal array variables point to an array object. Of course this is when the Java garbage collection mechanism discovers the array object that no one is referencing and then takes it away. As you can see from the above, "Michael", "Orson" and "Andrew" are all basic data types. But they are stored in heap memory. In fact, it should be said that local variables are placed in stack memory, (like the names[],animal[above) this type of reference variable, there are some basic types of variables, but the application variable refers to the object is stored in the heap memory. (including arrays and some common class objects that we normally write)Java objects in heap memory are usually not allowed to be accessed directly, but you can think of the consequences of direct access. To access objects in the heap memory, you need to refer to the variable as the mediator. When is a variable in Java stored in stack memory just a reference variable? When did it change its identity into a real Java object? Well, take a look at the following example:
Copy Code code 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) {
Dynamic initialization of an 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 an array variable refers to the object's method (or property), it becomes the actual Java object
System.out.println (animal.length);
Dog This object reference originally stored in stack memory becomes the actual object by invoking the object's method
Dog.info ();
Animal[0].info ();
}
}
Only when the reference variable in the stack memory invokes the object's method, or when it points to the object's properties, it is truly an object from the variable. (for example, Cat,dog object reference variable, animal[] array variable in the example above). Through animal[0] = dog;
ANIMAL[1] = cat; All two variables point to objects stored in the heap memory, so the information they print is exactly the same.
The blue line in the above figure is the assignment statement: animal[0] = dog;
ANIMAL[1] = cat; Before the variable points to the state, the red dotted line is the state after the assignment statement, animal[0] and dog, animal[1] and cat are all pointing to the same heap memory space.