Get to know more about arrays from java-14.2
In this section, we will take a full look at arrays.
1. Before array initialization, we cannot use its reference to do anything.
package com.ray.ch14;public class Test {public static void main(String[] args) {int[] a;// System.out.println(a);//error,The local variable a may not have been// initialized// a[0]=0;//error,The local variable a may not have been initialized}}
The error message above illustrates the point of view.
2. when initializing an array, if it is a numeric type in the basic type, each element is initialized to 0. If it is a char, It is 'null'. If it is a boolean, it is false, if it is another type (including the basic type that is not a numeric type), each element is initialized to null.
package com.ray.ch14;import java.util.Arrays;public class Test {public static void main(String[] args) {int[] a = new int[3];String[] b = new String[3];char[] c = new char[3];boolean[] d = new boolean[3];MyClass[] myClasses = new MyClass[3];System.out.println(Arrays.toString(a));System.out.println(Arrays.toString(b));System.out.println(Arrays.toString(c));System.out.println(Arrays.toString(d));System.out.println(Arrays.toString(myClasses));}}class MyClass {}
Output:
[0, 0, 0]
[Null, null, null]
[,]
[False, false, false]
[Null, null, null]
3. Two methods to create an array: explicit new and implicit new
package com.ray.ch14;public class Test {public static void main(String[] args) {MyClass[] a = new MyClass[3];MyClass[] b = { new MyClass(), new MyClass(), new MyClass() };}}class MyClass {}
The above two creation methods are equivalent.
4. The above implicit new is actually clustering initialization, but it has some limitations, that is, it must be initialized at the current position. Let's look at another dynamic clustering initialization.
package com.ray.ch14;public class Test {public static void main(String[] args) {MyClass[] a = new MyClass[] { new MyClass(), new MyClass(),new MyClass() };MyClass[] b = { new MyClass(), new MyClass(), new MyClass() };}}class MyClass {}
Observe the above Code, array a is more flexible.
5. the array identifier is a reference pointing to objects in the heap.
package com.ray.ch14;public class Test {public static void main(String[] args) {int[] ints = new int[] { 1, 2, 3 };System.out.println(ints);}}
Output:
[I @ 1bab50a
The output above is actually a memory address.
6. The basic type array stores the basic type value, and the object array stores the object reference (memory address)
package com.ray.ch14;public class Test {public static void main(String[] args) {int[] ints = new int[] { 1, 2, 3 };for (int i = 0; i < ints.length; i++) {System.out.println(ints[i]);}MyClass[] myClasses = new MyClass[] { new MyClass(), new MyClass(),new MyClass() };for (int i = 0; i < myClasses.length; i++) {System.out.println(myClasses[i]);}}}class MyClass {}
Output:
1
2
3
Com. ray. ch14.MyClass @ c3c749
Com. ray. ch14.MyClass @ 150bd4d
Com. ray. ch14.MyClass @ 1bc4459
Summary: The preceding section summarizes multiple aspects of the array and provides examples for each aspect.
This chapter is here. Thank you.