In Java, what would a variable of type int
hold if it was not initialized (I know that it will not let me compile if I used x
directly before initi
for int x
the default value would be 0
For primitive types please refer to this link
and for int[] x
would be null
Instance variables will be defaulted to a 'reasonable' value. Local variables will contain garbage.
Read up on the topic here.
x in "int x" is automatically initialized as 0; x in "int [] x" is automatically initialized as null, since x is actually a reference
but compiler will prompt to initialize the variable:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable a may not have been initialized
The local variable a may not have been initialized
at initialization.main(initialization.java:6)