Java array definition and initialization

Array definition and initialization

The concept of array and points for attention

Definition of array

// 定义数组
char[] initArrays;
char initArrays1[];

Initialization of array

form

//静态初始化
char[] initArrays0;
initArrays0 = new char[]{'1','2'};
//char[] initArrays0= {'1','2'};

Static initialization needs to explicitly specify the initial value of each array element artificially, and the array length is determined by the system.

type[] arrayname={element1,element2...}; Is a simplified syntax format for array static initialization in the above two statements.

Note: array elements are enclosed by curly braces and separated by commas.

//动态初始化
int[] arrays;
arrays = new int[3];
//int[] arrays = new int[3];

Dynamic initialization is to artificially specify the array length, and the system allocates the initial value for the array elements.

Similarly, dynamic initialization has a simplified format: type [] arrayname = New Int [length];.

Note: the length of the array needs to be specified in square brackets.

Initial value of dynamic allocation

Since the dynamic initialization array only needs to specify the memory space required by each element, and the system assigns initial values to each element, how does the system assign these initial values. The following is an attempt to assign initial values to different types of arrays:

int[] arrays = new int[3];
boolean[] arrays1 = new boolean[3];
String[] arrays2 = new String[3];
float[] arrays3 = new float[3];
char[] arrays4 = new char[3];
System.out.println(arrays[0]);//0
System.out.println(arrays1[0]);//false
System.out.println(arrays2[0]);//null
System.out.println(arrays3[0]);//0.0
System.out.println(arrays4[0]);//输出'\u0000'为空
}

Then make a summary according to the search data:

Towards Java: an in-depth understanding of Java arrays

Tips: Thoughts on the null output '\ u0000': what are the differences between various nulls ("", \ u0000, null) in Java?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>