1. Arrays and array elements
Arrays in Java are reference types
The elements of an array can be basic types or references Type, clarifying the type of array elements can help us understand the default initialization of array elements
2. One-dimensional array
The default initialization value of one-dimensional array elements is divided into There are two types, the elements are basic data types and reference data types
1. When the elements are basic data types
- ##Integer types (byte, short, int, long ) The default initial value is 0
- Floating point type (float, double) The default initial value is 0.0
- Boolean type (boolean) The default initial value is flase
- Character type (char) The default initial value is 0 (null character)
Note that the 0 here is different from the 0 of the integer type and not the character "0". This refers to the decimal 0 in the ASCII code table below
##Now we test the following code to deepen our understanding
public class Test {
public static void main(String[] args) {
char[] a = new char[2];
if(a[0] == 0) {
System.out.println("这是判断0的" + a[0] + "测试!");
}
if(a[0] == '0') {
System.out.println("这是判断字符'0'的" + a[0] + "测试!");
}
}
}
Copy after login
The running results are as follows
Through the results we found The value of a[0] is judged to be 0 instead of the character '0'. When printed, a[0] is actually a null character (this is not a space!!!)
2. The array elements are reference types When
the element is a reference type, the default initial value is null
3. Two-dimensional array
A two-dimensional array in Java actually uses a one-dimensional array as an element of the array The interpretation of the default initialization value of the two-dimensional array composed of
is divided into two situations (the writing format of the following two situations is represented by int, and int can be replaced by other data types)
1 . int[][] arr = new int[2][2]
①Outer element (arr[0], arr[1]): address value ②Inner element (arr[0][0]): Use the default initial value of the one-dimensional array to determine 2. int[][] arr = new int[2][]
①Outer element (arr[0], arr[1]): address value ②Inner element (arr[0][0]): Null pointer exception We understand it with the following figure, because the two-dimensional array is actually an array of arrays, and the outer layer saves is the address value, the inner layer can naturally judge based on the one-dimensional array
The above is the detailed content of Explanation on how to determine the default initialization value of Java one-dimensional array and two-dimensional array elements. For more information, please follow other related articles on the PHP Chinese website!