Why is it that when an array of 'ints' is made as a local variable they default to zero?
public static void main(String [] args) {
int []arrayInts = new int[5];
for(int i: arrayInts)
System.out.println(i);
//Prints out zeros
}
While if an 'int' variable is declared as a local variable it does not get initialized.
public static void main(String [] args) {
int a;
System.out.println(a);
//Compilation error: The local variable a may not have been initialized
}
neware always initialized. Java is 100% consistent in that regard.newkeyword initialises all objects created with it by default. "Stack" allocated objects are not initialised by default because the compiler can check at compile time if there's a danger of you using a stack variable before it's initialised, the same is not true of variables whose memory is dynamically allocated.