in Java is an ordered collection of the same type of data. The array describes several pieces of data of the same type, combined in a certain order. Each of these data is called an array element, and each array element can be accessed by a subscript. Three characteristics of the array:

  • Its length is determined, once the array is created, its size cannot be changed.
  • Its elements must be of the same type, and mixed types are not allowed. Elements in the
  • array can make any data type, including basic types and reference types. The

array belongs to reference type.

Array declaration: There are two forms of array declarations in Java.

int a[];
int[] a;

The first one is the usual way of declaring in C/C++. Array object creation:

a = new int[4];

Array initialization method: 1. Default initialization: The array element is equivalent to the member variable of the object. The default value is the same as the member variable (0;\u0000;null;false) 2. Static initialization:

        a[0] = 1;
        a[1] = 4;
        a[2] = 7;
        a[3] = 5;


int[] a = {1,2,3,4};

3. Dynamic initialization:

for(int i = 0;i<a.length;i++) {
            a[i] = i;

Below we create an array of custom types.

class Car {
    String name;
    Car(String name){ = name;
public class testArray {
    public static void main(String[] args) {
        Car[] car = {
                new Car("大众"), new Car("宝马"), new Car("比亚迪")
                };for(int i = 0;i<car.length;i++) {
            String a = car[i].name;
 * output:
 * Volkswagen
 * BMW
 * BYD