Detailed Explanation of Dynamic Initialization of Java Array

  • 2021-11-24 01:20:51
  • OfStack

Concept

1. Array dynamic initialization only gives array length, and the system defaults to initialization value.

2. Format

Data type [] Array name = new data type [Array length];


int[] arr = new int[3];

Instances


package com.itheima.array;
 
public class Demo2Array {
    /*
         Dynamic initialization of arrays :
                         At initialization time ,  You need to specify the length of the array manually ,  The system assigns an initial value to the array container .
 
         Dynamic initialization format :
                         Data type []  Array name  = new  Data type [ Length of Array ];
 
         Attention :
                         When printing array variables, ,  The memory address of the array will be printed out 
 
        [I@10f87f48 :
 
                        @ :  Separator 
                        [ :  The current space is 1 Array type 
                        I :  The type of data stored in the current array container 
                        10f87f48 : 106 Binary memory address 
 
                                0 1 2 3 4 5 6 7 8 9 a b c d e f
     */
    public static void main(String[] args) {
        //  Data type []  Array name  = new  Data type [ Length of Array ];
        //  Pass new Keyword creates the 1 A int Array container of type ,  This container can store 5 A int Integer of type ,  The container is arr Array variable 
        int[] arr = new int[5];
        // [I@10f87f48
        System.out.println(arr);
 
        byte[] bArr = new byte[3];
        // [B@b4c966a
        System.out.println(bArr);
 
    }
}

Extension of knowledge points:

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 an array

"1" Dynamic initialization only specifies the length, and the initialization value is given by the system

int[] arr = new int[5];

"2" Static initialization gives the initialization value, and the length is determined by the system

3. Dynamic initialization format:

Data type [] Array name = new data type [Array length];


Related articles: