Zen House Moon (http://www.cnblogs.com/yaoyinglong/) static Java array
The Java language is a typical static language, so an array is also static, that is, when the array is initialized, the length of the array is immutable.
The Java language array variable is a reference type, what does it mean? This means that the array variable is not the array itself, it simply points to the array object in the heap memory. Such as:
The 3 variables and the allocations in memory of the arrays of the respective references
We can usually change the length of an array by changing the direction of the array variable so that it points to other array objects in the heap memory (provided that their compilation type is compatible). But actually we just let the array variable point to another Array object, and the original array object waits for garbage collection without any other array variables pointing to it.
In the case of an array variable, it does not need to do so so-called initialization, but it simply points to a valid array object.
For a Java program, all variables of the reference type do not need to be initialized, only the object referenced by the reference variable that needs to be initialized.
All local variables are stored in the stack memory, regardless of whether they are variables of a reference type or basic types, and they are in the respective method stack, but refer to the object referenced by the type variable (including arrays, Normal Java objects) are always stored in heap memory.
For Java, objects in heap memory, whether arrays or plain Java objects, are generally not allowed to access directly, and in order to access objects in the heap memory, they are usually only referenced by reference variables.
Array variables are stored in the stack memory, but the array elements are kept in heap memory as part of the array object, whether they are array elements of the base type or array elements of reference types. Initialization of an array of primitive types
For a primitive type array, the value of the array element is stored directly in the corresponding array element, so the program allocates memory space directly to the array, and then stores the value of the element in the corresponding memory.
Intarr is just an array variable that exists in the method stack where it is, and it actually points to an array object in memory. Therefore, the array variable should be separated from the array object. Initialization of an array of reference types
is a bit more complicated than the primitive type array initialization: the reference type array element is stored or referenced, rather than referencing the object itself, which points to another piece of memory in the heap (which stores the object that the reference variable points to: an array or a Java object).
Public classPerson {PrivateString name; Private intAge ; PublicPerson (String name,intAge ) { This. name=name; This. age=Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString toString () {return"My name is" +name+ "my Age is:" +Age ; } Public Static voidMain (string[] args) {person[] students=NewPerson[3]; Person Xiaoming=NewPerson ("xiaoming", 10); Person Xiaohong=NewPerson ("Little Red", 15); Person Xiaozhang=NewPerson ("Xiao Zhang", 20); students[0]=xiaoming; students[1]=Xiaohong; students[2]=Xiaozhang; SYSTEM.OUT.PRINTLN (arrays.tostring (students)); System.out.println (Does "Xiaoming and students[0" point to the same Java object? -"+ (xiaoming==students[0])); Xiaohong.setname ("My name's changed."); System.out.println ("The Name property of the Java object that it points to is modified by the variable xiaoming:" +xiaohong.getname ()); System.out.print (The Name property of "Get Students[1" is: "); System.out.println (students[1].getname ()); }}
The results are printed as follows:
The memory of the main function execution process:
Arrays and their memory controls