I was just checking some OCJP questions and came across this difference during String array initialization and exceptions.
Case 1
try {
String[][] b = new String[10][10];//1
System.out.println(b[0][0]);//2
} catch (Exception e) {
System.out.println("Exception during array 'b' initialization");
e.printStackTrace();
}
Case 2
try {
String[][] a = new String[10][];//3
System.out.println(a[0][0]);//4
} catch (Exception e) {
System.out.println("Exception during array 'a' initialization");
e.printStackTrace();
}
Line 2 does not throw any exception whereas line 4 throws a null pointer exception.
Line 2 does output the value as null
though.
Does java have a difference in default values for initialization when the size of an array is specified and when its not?