Preface
When I recently used the ACM topic in Java, I often forget how the array implements static initialization, so here is the common sense used by the Java array.
Java Array Common sense
An array is an object in Java, and an array instance needs to be created with the new operator.
Common sense Knowledge:
- The size of the array can be obtained by Array.Length.
- The array index starts with 0 and the negative index is invalid in Java. Also, unlike C-language arrays, arrays in Java are judged out of bounds, and invalid indexes throw arrayindexoutofboundexception.
- An array of contiguous memory spaces stored in the Java heap.
- An array is a fixed-length data structure, and once the length of the array is declared, subsequent operations cannot change the length of the array.
- Arrays are very fast data structures, and if you can already determine how much data you have, you should use arrays instead of data structures such as ArrayList.
initialization of Java arrays
The initialization of Java arrays is divided into dynamic initialization and static initialization.
As an example of an array of type int, the static initialization and dynamic initialization are described separately.
Static Initialization
intnewint[] {123456};
As you can see from the code above, static initialization is the initial value of each element of the specified array, and the system determines the length of the array.
Dynamic Initialization
intnewint[6];
As you can see from the code above, dynamic initialization is just specifying the length of the array, and the system assigns the initial values to the arrays.
Different types, the initial value of the system allocation is not the same.
| type |
Initial value |
| Integer type (byte,short,int,long) |
0 |
| Floating-point type (float,double) |
0.0 |
| Character type (char) |
' \u0000 ' (representing spaces) |
| Boolean Type (Boolean) |
False |
| Reference type (class, interface, array) |
Null |
Java Array Memo