Please see the following code:Concept of arrays is clear to me but when it comes to these kind of questions i am stuck.I think I am messed up with arrays.Please clear my understanding.
1. class Dims {
2. public static void main(
String[] args) {
3. int[][] a = {{1,2,}, {3,4}};
4. int[] b = (int[]) a[1];
(---> not clear a[1] which is the 2nd element of 2 dimensional array 'a' should be a ref to an array of int then how this assignment is valid) 5. Object o1 = a;
( a is a reference to 2 dim array and its being assigned to an object o1) 6. int[][] a2 = (int[][]) o1;
(---> again the same doubt as above) 7. int[] b2 = (int[]) o1;
8. System.out.println(b[1]);
9. } }
The output is:
A ClassCastException is thrown at line 7 because o1 refers to an int[][]
not an int[]. If line 7 was removed, the output would be 4.