Initialization of an int array

I have a simple Java related question. Say you have an int array as an instance variable:

int[] in = new int[5]; 

So, now by default it contains 5 zeros. But what if you have the same array as a local variable. Do I need to initialize zeros? This is not homework; I am learning the Java language. Regards

+20
java arrays initialization int default-value
Nov 22 '12 at 11:23
source share
7 answers

The first thing to understand is that local variables are stored on the stack, which are not explicitly initialized with their default values. Although instance variables are stored in Heap, they are initialized by default to the default value.

In addition, objects are also created on the heap, regardless of whether the reference instance variable contains its own link or local reference variable.




Now, when you declare a reference to an array like this as a local variable and initialize it with an array: -

 int[] in = new int[5]; 

A reference to the array (in) is stored on the stack, and memory is allocated for the array that can hold 5 integer elements on the heap (remember, objects are created on the heap). Then, 5 contiguous memory locations (size = 5) allocated on the heap to store an integer value. And each index of the array object contains a link to this memory cell in sequence. Then the array refers to this array. Thus, since memory for 5 integer values ​​is allocated in Heap, they are initialized with a default value.

And also, when you declare a reference to an array and do not initialize it with any array object: -

 int[] in; 

An array reference is created in Stack (since it is a local variable), but by default it is not initialized by the array, and not to null , as is the case with instance variables.




So, here is what the distribution looks like when you use the first way to declare an array and initialize: -

 "Your array reference" "on stack" | | "Array object on Heap" +----+ | in |----------> ([0, 0, 0, 0, 0]) +----+ "Stack" "Heap" 
+45
Nov 22 '12 at 11:36
source share

This is the same:

int[] in = new int[5] as an instance variable or local variable. An in array object will contain zeros in both cases.

The difference would be if you did something like:

  • Instance variable: int[] in ; (it is initialized to null ), and the in object will be in heap space.

  • Local variable: int[] in ; (it must be initialized by the user) will live on the stack

+5
Nov 22 '12 at 11:35
source share

For arrays of primitive types, they are initialized with default values. The documentation says:

a one-dimensional array of the specified length is created and each component of the array is initialized with a default value

For an integer value, the default value is 0.

+3
Nov 22 '12 at 11:37
source share

Yes, when you initialize the array, the contents will be set to the default value for this type, for int it will be 0, and for the reference type it will be null .

If you initialize the array and check the contents, you can see it 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 
+2
Nov 22 '12 at 11:35
source share

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 to do is initialize the array . after voila array initialization, your array element ** will get its default values .

+1
Nov 22 '12 at 11:25
source share

It does not matter if the declared array in the instance variable or local variable is initialized to the default value.

Each class variable, instance variable, or array component is initialized with a default value when it is created.

By JLS

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

+1
Jan 05 '14 at 9:36 on
source share

An array does not contain 5 zeros when creating it as a local variable.

0
Nov 22 '12 at 11:25
source share



All Articles