What is the memory model of the Java object array?

Which of the following is true of the

Java reference type array memory model? Does each element of the object array have a reference in stack memory?

read the tutorials of the two teachers, and draw memory diagrams a little different

related codes

Person[] per = new Person[]{
        new Person("A", 1),
        new Person("B", 2),
        new Person("C", 3)        
    };

version 1:
1

2:

I personally think both are right, but the first is the simplified version, if according to my understanding, it is more inclined to version 2. Just a little confused, the array stores anonymous objects, will anonymous objects have references in the stack?

Apr.04,2022

1 is right and 2 is wrong. Java objects are allocated in the heap, and arrays are objects, so the assignment in the heap


version 2 is directed against him.


figure 1 is correct. There are no references such as per [0], pr [1], per [2] in the stack. Take a look at

Person[] per = new Person[]{
        new Person("A", 1),
        new Person("B", 2),
        new Person("C", 3)        
    };

this code accounts for only one slot in per in the local variable table. Locals=1,
you can use javap-v to take a look at


this has nothing to do with whether it is JAVA or not.
this is actually the concept of a pointer. Only the header address of the array is stored in the stack. Per [1], per [2] is actually the address + 1 + 2, and there is no specific per [1], per [2] variable

.
Menu