Home  >  Article  >  Java  >  Java implements dynamic initialization of arrays

Java implements dynamic initialization of arrays

王林
王林forward
2019-11-30 16:23:343052browse

Java implements dynamic initialization of arrays

1. What is the initialization of an array?

is to open up a continuous memory space for the array and assign a value to each array element.

2. How to initialize the array

1. Dynamic initialization

Only specify the length, and the system will initialize it Value

int[] arr = new int[5];

Recommended related video tutorials: java video tutorial

2. Static initialization

gives the initialization value, given by The system determines the length

3. Dynamic initialization format:

数据类型[] 数组名 = new 数据类型[数组长度];

4. Case:

Integer type: byte, The default initialization values ​​of short, int, and long are all 0

Floating point type: The default initialization value of float and double is 0.0

Boolean type: booleanThe default initialization value is false

Character type: char default initialization value '\u0000'

char: The two bytes occupied in the memory are 16 binary bits

\u0000: Each 0 actually represents Hexadecimal 0, then four 0s represent 16 binary digits

[I@19bb25a: [represents an array, a few represent several dimensions, I represents int type, @ is fixed, 19bb25a represents the address value of the array

The example is as follows:

class Demo2_Array {
    public static void main(String[] args) {
        //数据类型[] 数组名 = new 数据类型[数组长度];
        int[] arr = new int[5];                //动态初始化,在内存中开辟连续的5块空间
        System.out.println(arr[0]);            //系统给出默认初始化值,整数类型的都是0                                
        arr[0] = 10;
        System.out.println(arr[0]);    
        System.out.println(arr);            //[I@19bb25a
    }
}

For more related articles and tutorials, please visit: java introductory learning

The above is the detailed content of Java implements dynamic initialization of arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete