Important: Before using Java arrays, you must initialize the array object.
When all the elements of an array are allocated the appropriate memory space, and the initial values are specified, the initialization of the array is complete. The program will not be able to re-alter the position and size of the array object in memory.
Knowledge Point collation:
1. Arrays are initialized in the following two ways:
1) Static initialization: The initial value of each array element is displayed by the programmer at initialization, and the length of the array is determined by the system.
2) Dynamic initialization: When initializing, the programmer only specifies the length of the array, and the system assigns the initial values to the set of elements.
Regardless of the method used to initialize the Java array, the length of the array cannot be changed once the initialization is complete.
code example:
Public classDemo { Public Static voidMain (string[] args) {//use static initialization modestring[] Books =NewString[] {"1001 Nights", "300 poems of the Tang Dynasty", "Warning fable", "A man's Holy Face", }; //simplified form with static initializationString[] names = {"Little Red", "Xiao Ming", "Little Green", }; //in a dynamically initialized waystring[] Strarr =NewString[5]; //accessing the array length aboveSystem.out.println ("The length of the first array is" +books.length); System.out.println ("The length of the second array is" +names.length); System.out.println ("The length of the third array is" +strarr.length); }}
The Java language array variable is a variable of the reference type, Books,names,strarr the 3 variables and the allocations in memory of each of the referenced arrays
Summary: For static initialization, the programmer does not need to specify the array length, specifying the array elements of the array, which is determined by the system to determine the length of the array. That is, names is 3,books is 4.
When performing dynamic initialization, the programmer only needs to specify the length of the array, that is, to specify the required memory space for each array element, and the system will be responsible for assigning the initial values to the array elements. When you specify an initial value, the system assigns the initial value as follows:
1) The type of the array element is the integer type (byte,short,int and long) in the base type, and the value of the array element is 0;
2) The type of the array element is the character type (char) in the base type, then the value of the array element is ' \u0000 ';
3) The type of the array element is a floating-point type (float,double) in the base type, and the value of the array element is 0.0;
4) The type of the array element is a Boolean type (Boolean) in the base type, and the value of the array element is false;
5) The type of the array element is a reference type (class, interface and array), the value of the array element is null;
Java detail--Array and memory control