*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object points to double array, yet when casting it to single array, it causes Runtime error 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 "Object points to double array, yet when casting it to single array, it causes Runtime error" Watch "Object points to double array, yet when casting it to single array, it causes Runtime error" New topic
Author

Object points to double array, yet when casting it to single array, it causes Runtime error

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello,

I'm on the question/answer section of Bates/Sierra book. I am unable to understand a certain concept. Below is the code:




When I traced this code, I thought the output would be "4". However, the answer says:

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.


I really cannot understand this. Please explain how



is any different from



To me, it should be same thing because a double-array is being cast as single array.


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Hello Sandra, o1 is a two dimensional array (o1 = a;)
A two dimensional array cannot be assigned to a one dimensional array (int[] b2)
So the class cast exception is thrown at line 7
If you comment the line7, code runs and output would be 4

Hope this helps!


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561


is equivalent to


not:


Do you see the difference?
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello Pete,

I do see the difference.

a[1] itself is a one-dimensional array, whereas o1 is two-dimensional. The code would have been correct if we said



Now I have another question: Why do we even need to cast a[1] as (int[]); can't the compiler figure this out?


Thanks!
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello,

I ran the following code in NetBeans IDE:



The line

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

is giving compiler error. But isn't Object o1 pointing to the same reference that int [][] a is pointing to. And if

int[] b = (int[]) a[1];

is able to compile, why not

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

Is this because Object o1 is a Superclass of b2? Please clarify for us.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I'm no pro at this, but what if you cast it like so:


My rationale is that while o1 does refer to a 2-Dimensional int array, the variable is still nothing more than an Object variable, and that's all it knows that it is. So first you must cast it as a 2D int array (or a 1D Object array) before doing anything else to it, and then it will be smart enough to behave like a 2D int array or 1D Object array.

Another way that works but is more convoluted (and that I don't recommend) is like so:



as always corrections and clarifications are most welcome!
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello Pete,

I tried both statements and they compiled and ran!

Thank you for your explanation as well.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Sandra Bachan wrote:
Thank you for your explanation as well.


You're welcome, but also a warning: I'm not certified (except perhaps insane), and so my explanations are just how I see it, not the True Gospel™

Happy coding!
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Sandra : Mr. Stein has explained this well, but as he knows, I like to ruminate on
things a bit, especially since this S&B question stumped me too. My learning point
is that the compiler is all about variable type. It does not take the time to figure out
what object type will be assigned. So 'o1' as a Object can be down-cast to any type,
including an array. The rules for casting arrays is that they must share the same
dimension and be assignment compatible. For example: This is okay because 'bb' can access all Alpha[] methods, and Object[] methods
too. A tricky part of the S&B question is that the compiler polices the variable
types while assignment compatibility is a run time test.

Jim ... ...


BEE MBA PMP SCJP-6
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@Jim Hoglund

Respected Sir,

I think you must review the following line......



I think it should be



Thanks !!!

SCJP 6.0 96%

(Connecting the Dots ....)
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
You are absolutely correct. Sorry for the error. Jim ... ...
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Jim Hoglund wrote:Sandra : Mr. Stein has explained this well, but as he knows, I like to ruminate on
things a bit, ...


"Mr. Stein"? No need to be formal; you can just go back to calling me "sir".

But seriously,...

My learning point is that the compiler is all about variable type. It does not take the time to figure out
what object type will be assigned.

Not only does it not take the time, but AFAIK, it can't know at the time of compilation with 100% certainty.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object points to double array, yet when casting it to single array, it causes Runtime error