• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array- can't understand the code.

 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can some one please explain what's going on this code.
What does line 4 mean.
In line 5 why is a 2-dimensional array being assigned to a variable of type Object.
What is line 6&7 . Some kind of casting?

I got a runtime error(Class Cast exception) because of line 7 which if removed will give the output 4.

[Question from K&B]
 
Gaurav Joshi
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code Explained with comment

1 class Dims {
2public static void main(String[] args) {

3 int[][] a={ {1,2}, {3,4} }; // Creating two dimensional Array with
//a[0] = {1,2} and a[1] = {3,4}
4int[] b= (int[]) a[1]; // assigning a[1] to b
5Object o1=a; // Remember a is also an object so
// upcasting to Object
6int[][] a2=(int[][])o1; // assigning a to a2 with downcasting
7int[] b2=(int[]) o1; // Run Time exception as ol can only be
//type casted to 2- d array
8System.out.println(b[1]);
}

}

Thanks and Regards,
Gaurav Joshi
 
Nadeem Khan
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[][] a={ {1,2}, {3,4} };
//Declares an array object referred by a. a's array object contains references to two array objects:
a[0] --refers to--> {1,2}
a[1] --refers to--> {3,4}
Therefore we actually are creating 3 array objects on the heap here. One referred by a, second by a[0] and third by a[1].



4int[] b= (int[]) a[1];
// b refers to the 1D array object referred by a[1], so casting allowed & not necessary here.

5Object o1=a;
//Since any array object is also an Object, we can refer to a by an Object type variable o1.

6int[][] a2=(int[][])o1;
//Since o1 actually refers a 2D array type, we CAN cast it back to a 2D array type.

7int[] b2=(int[]) o1;
//Since o1 actually refers a 2D array type, we CANT cast it back to a 1D array type.

8System.out.println(b[1]);
//b --> a[1]. Hence b[1] = a[1][1] = 4!




Can some one please explain what's going on this code.
What does line 4 mean.
In line 5 why is a 2-dimensional array being assigned to a variable of type Object.
What is line 6&7 . Some kind of casting?

I got a runtime error(Class Cast exception) because of line 7 which if removed will give the output 4.

--#@ I hope that cleared a bit @#--
 
yu yong
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Joshi And Nadeem Khan`s reply are all right.
I think the problem is clear.
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot guys..That cleared alot!
Just one question..

Object o1=a;

Can a two dimensional array be assigned to o1 of type Object (which is not 2-dimensional) and simply an object.

int[][] a2=(int[][])o1;

Why do we need to cast o1(ie. a as Object o1=a) when it is already 2-dimensional.
[I am getting an error of incompatible type when I am trying to compile it with out casting.]


Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a two dimensional array be assigned to o1 of type Object (which is not 2-dimensional) and simply an object.


All arrays are objects. So, a two dimensional array, which is an array of array, is an object.


Why do we need to cast o1(ie. a as Object o1=a) when it is already 2-dimensional.
[I am getting an error of incompatible type when I am trying to compile it with out casting.]


All arrays are objects, but not all objects are arrays. So the Java compiler doesn't know if an object can be casted to an array, implicitedly.

Henry
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
That made it clear.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic