• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


is equivalent to


not:


Do you see the difference?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Pete,

I tried both statements and they compiled and ran!

Thank you for your explanation as well.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jim Hoglund

Respected Sir,

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



I think it should be



Thanks !!!
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are absolutely correct. Sorry for the error. Jim ... ...
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic