How to declare an array in java

Declaring an array
When declaring an array, you cannot specify its length (number of elements in the organization). An array is a variable of a reference type, so when you use it to define a variable, it only means that a reference variable (similar to a pointer) is defined, and the reference variable has not yet pointed to any valid memory, so you cannot specify the length of the array when defining an array.
int[5] intErrorArray;

Array Creation
To create an array, if you do not initialize the array at the time of creation, you must specify its size; if you do not specify the size, you must initialize the array. If you do not specify the size of the array, you must initialize the array while creating.

Initialization method:
1) Static initialization:

 int[ ]  intArray;
 intArray = new int[ ] {1,2,3,4,5};

2) Simplified static initialization method:

 String[] strArray = {"张三", "李四", "王五" };

3) Dynamic initialization:
The length of the array is specified during initialization, and the system initializes the default value of each array element.

int[] price = new int[4];

Arrays in java language must be initialized before they can be used. Initialization is to allocate memory space for the elements of the array, and assign an initial value to each array element.
The default value of the integer type (byte, short, int, long) of the
basic data type is 0; the default value of the floating point type (float, double) of the
basic data type is 0.0; the default value of the character type (char) of the basic data type is ' \u0000';
the default value of the boolean type of the primitive data type (boolean) is false; the default value of
the reference type of the type (class, array, interface, String) is null.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324122880&siteId=291194637