Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int array initialization

I have here a simple question related to Java. Let's say you have an int array as instance variable:

int[] in = new int[5];

So, now by default it contains 5 zeros. 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. Best regards

like image 457
uml Avatar asked Nov 22 '12 11:11

uml


People also ask

How do you initialize an integer array?

To use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets containing its size: int[] intArray = new int[10]; This allocates the memory for an array of size 10 . This size is immutable.

How do you initialize an array?

The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.

Is an int array initialized to 0?

By default in Java, data types like int, short, byte, and long arrays are initialized with 0.

What is an int array initialized to in Java?

For object of integer array type all values in the array are initialized to 0(zero) in the constructor method. Similarly for object of boolean array, all values are initialized to false. So Java is initializing the array by running its constructor method while creating the object.


3 Answers

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.

Also, objects are also created on Heap, regardless of whether an instance reference variable is holding its reference, or a local reference variable.


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). Then, 5 contiguous memory location (size = 5), for storing integer value are allocated on Heap. 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.

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.


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" 
like image 166
Rohit Jain Avatar answered Sep 25 '22 20:09

Rohit Jain


It is the same thing if you do :

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

Difference would be if you would do something like :

  1. Instance variable : int[] in; (it is initialized with null), and the in object will live in heap space.

  2. Local variable : int[] in; (it has to be initialized by the user) will live in stack

like image 43
Ioan Avatar answered Sep 22 '22 20:09

Ioan


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.

like image 43
Parvin Gasimzade Avatar answered Sep 22 '22 20:09

Parvin Gasimzade