Click to enter _ more _java thousand ask
1. When the memory space of an array is allocated
An array in Java is a data structure used to store the same data type, and once the initialization is complete, the space occupied is fixed, and the process of initializing it is the process of allocating the corresponding memory space. Even though an element is emptied, its space remains, so the length of the array cannot be changed.
Learn what an array looks here: What is an array in Java
When you define only one array variable (int[] numbers), the variable does not point to any valid memory, so you cannot specify the length of the array, only the arrays are initialized (the array element allocates memory space) before it can be used.
Array initialization is divided into static initialization (specifying the value of the array element at the time of definition, the array length cannot be specified) and dynamic initialization (specifying only the array length, the system assigns the initial value).
//静态初始化intnewint351287"Miracle""Miracle He" };//使用静态初始化的简化形式//动态初始化intnewint[5new String[2];
It is recommended that you do not mix static initialization with dynamic initialization, that is, do not specify both the length of the array and the value of each element.
When the initialization is complete, the array elements can be accessed by index location (0~array.length-1).
When using dynamic initialization, such as the corresponding index bit does not specify a value, the system will specify the corresponding data type corresponding to the default value (integer 0, floating-point number is 0.0, the character is ' \u0000 ', the Boolean type is false, the reference type (including string) is null).
Public classTestarray { Public Static void Main(string[] args) {string[] names =Newstring[3]; names[0] ="Miracle"; names[1] ="Miracle He";/* for (int i = 0; i < names.length;i++) {System.out.print (Names[i] + ""); } */ //You can also use foreach to traverse for(String name:names) {System. out. print (name +" "); } }}
The results are as follows:
Miracle Miracle He NULL
Miracle Miracle He NULL
2. How the array is stored in memory
First, arrays (array references and arrays of elements) are stored in memory,
The array reference variable is stored in the stack memory (stack), and the array element is essentially an object that is stored in the heap memory. Access is achieved by pointing the pointer in the stack memory to the location of the corresponding element in the heap memory.
Understanding heaps and stacks look here: [What is the difference between Java heap and stack][3]
[3]:
When the array is initialized, the corresponding space is allocated in the heap, which is not changed by the internal elements, that is, if an element in the array is emptied, the memory space occupied by the array will not shrink.
Stores the reference type array in memory how to store it here: [How the reference type array is stored in memory][4]
[4]:
Java FAQ _06 Data structure (014) _java how arrays are stored in memory