Study Notes - how to understand the array (JAVA)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42110567/article/details/80517249

                                                How to understand the array (JAVA)

First thought a few questions:

1. There are several ways to initialize an array?

2. Be sure to initialize the array it? It called to initialize the array initialization exactly what? It is an array variable itself?

3. declare an array process, how memory is allocated?

4 basic types of arrays and memory allocation mechanism between the type of array initialization references What is the difference?


        An array is a composite structure are present in many programming languages, he may be used to store a plurality of variables of the same type. array java program must be initialized before you can use , so-called initialization, that is, and specify the initial value for each array element is an element allocates memory array object, but in Java, arrays are static, once the array initialization, length is already determined, can not be altered.

        Initialize the array is divided into two :( declaration has the following two examples included, if the actual process only declare arrays, such as: int [] a; and the array can not be used directly, because there is no allocation of memory space, then you need to initialize the array)

    Static initialization: determining by the program ape their initial value of each array element, and the length of the array by the system to decide, for example:

int[] a = {0,1,2,3,4};
System.out.println(a.length);

an array of a static initialization by the programmer, determines the value of each element, we did not specify the length of the array, but the system will automatically calculate the length, the result is 5 runs

    Dynamic Initialization: length of the array is determined by the initialization program apes, and not a value of the array element, assigned by the system to the initial value, for example:

int[] b=new int[5];
System.out.println(b[0]);

We will array b is initialized to an array of length 5, but does not specify a value for each element, then the output b [0] to 0 will, as we in the dynamic initialization int type array, the system automatically assigned the default value is 0, if not int type, but other types are:

            ■ array element type is the default value of integer type (byte, short, int, and long) basic types, the array element is 0.
            ■ Type array element type is the default floating-point (float, double) basic types, the array elements is 0.0.
            ■ array element type is the default type of base character type (char), the array elements are '\ u0000' ( the Unicode character code, represents a space).
            ■ Type array elements are Boolean type (boolean) basic types, the array element value is false.

            ■ array element type reference types (classes, interfaces, and arrays), the array element value is null.

We look at the second question:

        A lot of people would certainly say yes, you must first initialize the array to use before I also particularly marked in red font this sentence. But the answer is not necessarily required, in order to understand this problem, we need to understand the array variable and the array object distinction. And before you know they need to understand java data types. java data is divided into two types, basic data types and the reference data type.

The basic data types are eight: byte, short, int, long , float, double, boolean, char here do not do too much description, only need to know the basic data type variable contains a value, this type of variable values corresponding.Declare basic data types, the system automatically allocates a block of memory, then the program can operate directly on the stack assignment, and stored on the stack; Basic types of data stored on the stack memory in this sentence is wrong can only say that all local variables are stored in the stack.

Reference data types: reference value variable with a different basic types of variables, the variable value is a reference pointing to the memory space (address). Points to memory holds a value or variable represented by a set of values. This is a pointer in C language is very similar, in fact, is actually a reference pointer java language, is a pointer to an object memory address. java not support said pointer calculation but does not support a pointer, but the type of pointer is retained, and is called a reference type. After declaring the reference data types, it can not be used directly, but also instances of open space in the memory heap memory.


Array variable is a reference type variable, an array variable is an array object pointing to the heap memory of them, not the array itself. When changing the array referenced by an array variable, it can cause the array of variable length artifacts. In fact the length of the array object itself does not change this, but the array variable to point to a new array object.

So for the array variable, he does not need to be initialized, in fact, we often say that the initialization is to initialize an array of objects rather than an array of variables, sometimes we are not initialized, and let the array variable points to a valid object array, the array can also be used ,E.g:

int[] a = {0,1,2,3,4};
int[] b;	
b=a;	
System.out.println(b[1]);	
b[1]=99;	
System.out.println(a[1]);

Let a static initialization of the array, and then declare an array b, then b is assigned to array a, b, and this time the array variable array variables at the same time a point to an array of objects, the output b [1] will be 1, without error. In order to verify the value we can b [1] was changed to 99, then the output a [1] will also be 99.

---------------------------------------------------------------------------------------------------------------------------------

When you declare an array, the first assignment in a stack memory array variable is stored on the stack, and then open up space in the heap wait until initialize an array, the array variable to point to an array of objects. (Welcome to add ....)

---------------------------------------------------------------------------------------------------------------------------------

Initialization primitive array: the array element is stored directly in the corresponding array elements, a first program memory space allocated for the array, then the values stored in the array element corresponding to the amount of memory, relatively easy to understand.

The value of the variable in all basic types are present in the stack memory.This sentence is wrong, for example static initialization array, the elements are of a type int int type is a value type of the basic, but was present in the heap memory. The correct statement is as follows:

        All local variables are present in the stack memory, whether it is a basic type of variable or reference variable type, are stored in the respective process stack region; but the object reference type variable references (including arrays, ordinary Java objects) it is always stored in the heap memory.

Initialize an array of basic types: reference array element type is still an array of reference types, the array elements point to another piece of storage memory efficient object.



Guess you like

Origin blog.csdn.net/weixin_42110567/article/details/80517249