• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in K&B self-test

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Hi!




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!

 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,


Thanks srinivasan!

orginally posted by srinivasan..

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

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

thanks!
regards
samura

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

Explanation:

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 .


understood...

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 ...

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

understand...
[ August 19, 2007: Message edited by: sandeep atluri ]
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks
regards
samura




 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic