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 def
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"
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
An array initializer creates an array and provides initial values for all its components.
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. once you initialize you array, voila your array element**s get their **default values.
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
.
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
THe Array Doesn't Contain 5 zeroes when you instantiate it as a local variable.
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 :
Instance variable : int[] in
; (it is initialized with null), and the in
object will live in heap space.
Local variable : int[] in
; (it has to be initialized by the user) will live in stack