It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in K&B self-test 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 "doubt in K&B self-test " Watch "doubt in K&B self-test " New topic

doubt in K&B self-test

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111


why there is no compilation error on line no 8? instead it throws
classcastexception at run-time. how can we assign a 2-D array into an
1-D array by casting?

Thanks in advance!
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
int[]b2 = ((int[][])o1)[0];

remember o1 is an object.
so you can't call explicity like this o1[0]. // wrong

we should tell the compiler that
explicity that the refrence o1 is holding is pointing to 2D int array.

(int[][])o1 // this will do that

next out of 2D int array if want to fetch 1-D array.

((int[][])o1)[0] , ((int[][])o1)[1] ..... ((int[][])o1)[rowCount-1]
[ August 18, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111

Thanks srinivasan!

orginally posted by srinivasan..

int[]b2 = ((int[][])o1)[0];

but.. where does this line come from the program? I didn't get ...

sandeep atluri
Ranch Hand

Joined: Apr 19, 2007
Posts: 42
hi samura...

you asked where did this line come from

you have mentioned as follows in your code line 8:

now lets see....

o1 is a reference of type Object and is referring to an Object of type
at Compile time the compiler sees the referencing variable type only... not the object type...
so it doesn't find anything wrong in converting an Object type into an array(which is also an Object)....
but at runtime... the compiler realizes that its going to cast a 2D array into a 1D.. which cannot be done... since objects are immutable...

thats why there is a runtime error....

but remember one thing ... Object's members are not immutable... i.e.,they can be changed...
so what we have to do is use the members to create a new array object of 1D and assign this object to be referenced by the 1D variable b2.....

so... that is why sreenivasan used a 2D casting...

see here....
he is saying...

instead of

Did you see the difference...

sreenivasan has used the members in the 1 slot of the two dimensional array...

but you are directly casting the Object itself.....which creates a Runtime exception i.e., an Unchecked Exception

One more difference:.....

in your code even if you use as follows...

you will get a Compile time error saying that an int is submitted when an int[] should have been submitted...


here you are casting an object type into a 1D object when a 2d object is expected...

you are creating a new object that will have the values of a specific column or row of a 2D array.... so for that purpose you have to access these values... retrieve them and then assign this values into a new Object that is of 1D type array and then make it referred to by .


i know it's a little complicated.... read it twice and compare it to the question and the errors you get and you will understand ...

any more queries and you can ask me by mail...ok... bye

[ August 19, 2007: Message edited by: sandeep atluri ]

Sandeep Atluri
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi sandeep,

Thanks for the detailed explanation. this brought me more questions...
1. what is Object member here..(I guess it's a[0],a[1] correct me if am
wrong). int [][]a, int [][]a2, Object o1 all refer to the same 2-D int
array where as int []b refers only a[1](which is 3,4).

2. we didn't access or use this line of code
int[]b2 = (int[])o1;(just an assignment I guess..)

can you suggest an article for multidimensional assignments?


I agree. Here's the link:
subject: doubt in K&B self-test
It's not a secret anymore!