File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array- can't understand the code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array- can Watch "Array- can New topic
Author

Array- can't understand the code.

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661


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]


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Gaurav Joshi
Greenhorn

Joined: Mar 08, 2008
Posts: 18
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

Joined: Nov 27, 2007
Posts: 108
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 @#--


<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
yu yong
Ranch Hand

Joined: Mar 09, 2008
Posts: 44
Gaurav Joshi And Nadeem Khan`s reply are all right.
I think the problem is clear.


Yours sincerely,<br />yuyong<br /> <br /> <br /> E-mail:yuyong22@hotmail.com<br /> msn: yuyong22@hotmail.com<br /> Skype:yuyong88
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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
Sheriff

Joined: Sep 28, 2004
Posts: 19072
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks.
That made it clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array- can't understand the code.