Java array summary, java Array
--- Restore content start ---
1. Overview
Because java is a typical static language, arrays in java are also static; java array variables are reference type variables (ps: references in java are somewhat like pointers in c ++ and c) when we use java arrays, there are mainly two operations; define reference variables (equivalent to a pointer (NULL) defined in c ++/c) and instantiate an array object.
In the java memory allocation mechanism, defining local variables and referenced variables is to apply for a space in the corresponding method stack memory, and referencing the objects pointed to by variables (class objects and array objects) the heap memory is applied for; the stack memory is easy to access (direct access), and the heap memory is not easy to access (the heap memory needs to be accessed by referencing variables ).
2. array initialization java array initialization can be divided into two types: static initialization and dynamic initialization.
Static initialization:
Initialize the array object while defining the array;However, you cannot assign values to the array length..
Example: int [] a = new int [] {1, 2, 3, 4 };
String [] B = new String [] {"Kylin", "shanshan "};
Dynamic initialization:
When defining an array, only the array length is assigned a value, and no specific array object is initialized (by default)
For example, int [] c = new int [4];
String [] d = new String [3];
The values of the instantiated objects in the c array are c [0] = 0 c [1] = 0 c [2] = 0 c [3] = 0
D. The values of the array objects are null, null, and null;
Why? This is because java has a set of default initialization mechanisms. The following describes the initial values assigned by java to array elements:
The default value of the integer type (byte, short, int, double) is 0.
The default value of the floating point type (double, float) is 0.0.
Boolean default value: false
The default value of the reference type (array, class, interface, String) is null.
Default character type (char) Initial Value '\ u0000'
3. Two-dimensional array (multi-dimensional)
When Initialization is mostly an array, You can initialize only the leftmost dimension. java allows multi-dimensional arrays to be treated as a one-dimensional array.
Int [] [] a = new int [3] []; // The array name of the Two-dimensional array in java represents the first address of the one-dimensional array (same as c ++/c) system. out. println (. length); for (int I = 0; I <. length; I ++) {// points the elements of a one-dimensional array to a new one-dimensional array, forming a two-dimensional (multi-dimensional) array; here we can point to many new arrays with different lengths a [I] = new int [4] ;}for (int I = 0; I <4; I ++) {System. out. print (a [0] [I] + "");}
The running result is as follows:
--- Restore content end ---