简体   繁体   English

int数组初始化

[英]int array initialization

I have here a simple question related to Java. 我这里有一个与Java有关的简单问题。 Let's say you have an int array as instance variable: 假设你有一个int数组作为实例变量:

int[] in = new int[5];

So, now by default it contains 5 zeros. 所以,现在默认它包含5个零。 But what if you have the same array as local variable. 但是如果你有与本地变量相同的数组呢? Does it get initialized to zeros? 它被初始化为零吗? That is not a homework, I am learning Java language. 这不是作业,我正在学习Java语言。 Best regards 最好的祝福

First thing to understand is that, local varibles are stored on stack which are not initialized explicitly with their default values. 首先要理解的是, 局部变量存储在堆栈上 ,这些变量未使用其默认值显式初始化。 While instance variables are stored on Heap , and they are by default initialized with their default value . 实例变量存储在Heap上 ,默认情况下它们是使用默认值初始化的。

Also, objects are also created on Heap , regardless of whether an instance reference variable is holding its reference, or a local reference variable. 此外,无论实例引用变量是保持其引用还是本地引用变量,都会在Heap上创建对象


Now, what happens is, when you declare your array reference like this as local variable, and initialize it with an array: - 现在,当您将数组引用声明为局部变量时,会发生什么,并使用数组初始化它:

int[] in = new int[5];

The array reference (in) is stored on stack , and a memory is allocated for array capable of holding 5 integer elements on heap (Remember, objects are created on Heap). 数组引用(in)存储在堆栈中 ,并为数组分配内存,该数组能够在堆上保存5个整数元素(请记住,在Heap上创建对象)。 Then, 5 contiguous memory location (size = 5) , for storing integer value are allocated on Heap . 然后,在Heap上分配用于存储数值的5个连续存储器位置(size = 5) And each index on array object holds a reference to those memory location in sequence. 并且数组对象上的每个索引都按顺序保存对这些内存位置的引用 Then the array reference points to that array. 然后数组引用指向该数组。 So, since memory for 5 integer values are allocated on Heap, they are initialized to their default value. 因此,由于在Heap上分配了5个整数值的内存,因此它们被初始化为其默认值。

And also, when you declare your array reference, and don't initialize it with any array object: - 而且,当您声明数组引用时,不要使用任何数组对象初始化它: -

int[] in;

The array reference is created on Stack (as it is a local variable), but it does not gets initialized to an array by default, and neither to null , as is the case with instance variables. 数组引用是在Stack上创建的(因为它是一个局部变量),但它默认情况下不会初始化为数组,也不会像实例变量那样为null


So, this is how allocation looks like when you use the first way of array declaration and initialization: - 因此,当您使用第一种数组声明和初始化方式时,这就是分配的样子: -

"Your array reference"
     "on stack"    

       |    |          "Array object on Heap"
       +----+                  
       | in |---------->  ([0, 0, 0, 0, 0])
       +----+
       "Stack"                  "Heap"

It is the same thing if you do : 如果你这样做是同样的事情:

int[] in = new int[5] as instance variable or local variable. int[] in = new int[5]作为实例变量或局部变量。 The array object in will contain zeros in both cases. 在两种情况下,数组对象in将包含零。

Difference would be if you would do something like : 区别在于你是否会这样做:

  1. Instance variable : int[] in ; 实例变量: int[] in ; (it is initialized with null ), and the in object will live in heap space . (它用null初始化), in对象将存在于堆空间中

  2. Local variable : int[] in ; 局部变量: int[] in ; (it has to be initialized by the user) will live in stack (它必须由用户初始化)将存在于堆栈中

For primitive type arrays it is initialized to their default values. 对于基本类型数组,它被初始化为默认值。 In the documentation it says : 文档中它说:

a single-dimensional array is created of the specified length, and each component of the array is initialized to its default value 创建具有指定长度的一维数组,并将数组的每个组件初始化为其默认值

For the integer type default value is 0. 对于整数类型,默认值为0。

Yes, when you initialise an array the contents will be set to the default value for that type, for int it would be 0 and for a reference type it would be null . 是的,初始化数组时,内容将设置为该类型的默认值,对于int ,它将为0,对于引用类型,它将为null

If you initialise an array and inspect the contents you can see this for yourself: 如果您初始化一个数组并检查内容,您可以自己看到:

...
final int[] in = new int[5];

for (int i = 0; i < in.length; i++) {
    System.out.println(in[i]);
}
...

This will print: 这将打印:

0
0
0
0
0

yes

public void method() {
    int[] in = new int[5];
    System.out.pritnln(in[0]); //output in 0
}

In this case, your Array is a local variable , all you need is to initialize your array . 在这种情况下,您的Array是一个局部变量 ,您只需要初始化您的数组 once you initialize you array, voila your array element**s get their **default values . 一旦你初始化你的数组, 你的数组元素**得到他们的**默认值

It does not really matter whether the declared array in an instance variable or a local variable it will get initialized to the default value. 在实例变量或局部变量中声明的数组是否会初始化为默认值并不重要。

Each class variable, instance variable, or array component is initialized with a default value when it is created. 每个类变量,实例变量或数组组件在创建时都使用默认值进行初始化。

As per JLS 按照JLS

An array initializer creates an array and provides initial values for all its components.

将数组实例化为局部变量时,数组不包含5个零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM