Home > Java > JavaBase > body text

Java implements dynamic initialization of arrays

王林
Release: 2019-11-30 16:24:29
forward
3133 people have browsed it

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];
Copy after login

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 数据类型[数组长度];
Copy after login

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
    }
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template