Problems related to memory allocation based on Java arrays

  • 2020-04-01 01:57:03
  • OfStack

Java arrays are probably familiar to you, but I recently ran into a problem with memory allocation for Java arrays.
Ha ha. All of a sudden, the phrase "primitive data types are stored in stack memory and objects are stored in heap memory" in many books is completely wrong. Here is a simple example code:


public class Test {
    public static void main(String[] argv) {
//Statically initializes the array
String[] names = { "Michael", "Orson", "Andrew" };
//Initializes the array dynamically
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);
    }
}

"You can't change the size of a Java array." you've probably heard this before, but here's the problem: animal [] is 4, and names [] is only 3, but after an assignment, both arrays are 4. Doesn't that change the size of the array? The problem was in the way! Well, ask the techies, and you'll have a whole new understanding of how arrays are stored. The following is my understanding :(if there is a mistake, just by the god you see, also please be able to point out.
The names and animal above do not represent the array object, but merely the array variable, which is the same as the pointer in C. Such variables are called reference variables. Array objects are stored in heap memory. Of course, the size cannot be changed. However, array variables can point to other array objects.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013052115104851.png ">

The dotted blue line is the assignment statement names = animal; Previous names and animal array variables point to array objects in heap memory; The red line is names = animal; Then the names and animal array variables both point to an array object. And of course that's when the Java garbage collection mechanism finds that unreferenced array object and takes it away. As you can see from the top," Michael "," Orson","Andrew" are all basic data types. But they are stored in heap memory.   It should actually be said that local variables are stored in stack memory (such as names[], animal[], and other variables of reference type, as well as some primitive types), but the objects referred to by the application variables are stored in heap memory. Java objects in heap memory are usually not allowed to be accessed directly, but you can imagine the consequences of direct access. To access objects in the heap, you need to refer to the mediation variable. When is a Java variable stored in stack memory just a reference variable? When did it change its identity and become a real JAVA object? Well, here's an example:

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) {
//Initializes the array dynamically
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 references an object's methods (or properties), it becomes an actual Java object
System.out.println(animal.length);
//Dog, an object reference originally stored in stack memory, becomes an actual object by calling the object's methods
dog.info();
animal[0].info();
    } 
}

Only when a reference variable in the stack calls an object's methods or points to an object's properties does it become an object from a variable. For example, the cat,dog object reference variables in the above example, and the animal[] array variables.             Through animal[0] = dog;
Animal [1] = cat;             Makes both variables point to objects stored in heap memory, so they print the same information.
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013052115104852.jpg ">
The blue line in the figure above is the assignment statement:               Animal [0] = dog;
Animal [1] = cat;                   The previous variables point to the state, the red dotted line is the state after the assignment statement,animal[0] and dog,animal[1] and cat all point to the same heap memory space.

Related articles: