Today we have a look at the contents of an array in Java.
An array is a collection of ordered data that can be stored in an array to hold any data of the same data type, and the array itself is a reference type.
In the case of a one-dimensional array, the declaration of the array is written as the data type [] array name. This statement simply declares an array variable, does not create a real array, and cannot determine the length of the array, when the array cannot be accessed. After declaring the array, specify the size of the array, allocate the memory space to the array, and use the new operator to display the creation of an array that can be referenced by the definition and the operator new array to allocate space before referencing each element in the array. Of course, the Declaration of arrays and the size allocation of the array can be put together to complete, that is, type [] Arr_name = new Type[length]; the next step is to initialize the array, in Java, there are two types of initialization of arrays, static initialization and dynamic initialization. (in Java, for security purposes, to prevent memory loss, a default initial mechanism is provided for the created array, and three actions are completed after the creation of an array): 1. Create an Array object 2. Allocate storage space to the array in memory 3. Initializes a default value for the corresponding data type to the elements of the array. The so-called static initialization is to initialize the array when defining the arrays, for example: int k[]={1,3,5,7,9}; dynamic initialization is to separate the definition and spatial allocation of an array from the assignment of an array element, for example: Char ch=new char[3];ch[0]=a; Ch[1]=b;ch[2]=c;.
Let's talk about the memory space and memory allocation of the array. Let's first describe the areas that Java will cover in memory allocation: registers, stacks (which hold references to primitives and objects, but objects themselves are not stored in the stack, but are stored in the heap), and heaps (storing data generated with new).
The process of memory changes from the definition of an array of simple data types to the initialization is introduced first. When declaring an array, the system allocates the memory space for the array, which allocates a space in the heap memory space for the array reference variable, allocates space within the stack to the array object's reference, and when creating an array of simple data types, The system allocates the appropriate heap space to hold the memory space of the data type data, and assigns each element of the array an initial value that matches the set type, and assigns the values to the corresponding individual array elements when the arrays are initialized.
The process of referencing an array of data types from definition to initializing memory changes. The definition of a reference type array is basically the same as the definition of an array of simple data types; When created, it is the first time that the array elements are allocated a memory space and then assigned to the array elements a default initial value of NULL; when initializing, because the array itself is a reference type, and now the array element is the reference type, So the object referenced by the array element also allocates memory space.
The contents of an array in Java