Arrays 001

1.1 Array Initalization

  First of all, we need know Java arrays is static. When the array is initialized, the length of the array is immutable. The expansion of the arraylist is to create a larger array, and then copy the original array to the new array and add new elements. Java arrays must be initialzed before they can be used. Initialization is to allocate memory space for array objects.If we use arraylist but no explicit array length definition. The default length of the array is 10. 

  And Java array variables are reference variables. Using static initialization does not need to specify the length of the array, the system determines the length of the array. When performing dynamic array initialization, the system assigns default values to each array element.

  

  The array element type is an integer type in the basic type(byte, short, int, long), and the default value is 0.

  The array element type is a floating-point type in the base type(float, double), with a default value of 0.0.

  The array element type is a character type in the basic type(char), and the default value is \u0000.

  The array element type is a boolean type in the basic type, and the default value is false.

  The array element type is a reference type in the basic type, and the default value is null.

 

  Do not specify both the length of the array and the initial value of each array element when initializing the array.

  summary: Java arrays are static. Once the initialization of the array is complete, the space allocation of the array elements is ended. The program can only change the value of the array elements, and cannot modify the length of the array.

  An array variable in Java is a reference variable. And array variable is not an array, it just points to an array object in heap memory. Therefore changing the array referenced by an array variable will cause the illusion of variable array length.

  Initialization of basic data types, the values of array elements are stored directly in the corresponding array elements. The program directly allocates memory space for the array, and then stores the values of the array elements into the corresponding memory.

  All local variables are stored in the stack memory. Whether it is a basic data type variable or a reference type variable, it is stored in its own method stack area. Howerver, the objects referenced by reference type variables are always stored in heap memory. Object in heap memory can only be accessed through reference variable. A reference variable is essentially a pointer. As long as the program accesses the property or calls a method through the reference variable, the reference variable will be replaced by the object which it references.

  Because of the nature of data in heap memory, array objects in heap memory require array reference variable to access. Avoid direct access to the data in the heap memory in the Java language can ensure that the program is more rebust. The program cannot directly access and modify the data in the heap memory, which protects the integrity of the data in the memory.

Guess you like

Origin www.cnblogs.com/XieXiyu/p/12082728.html
001