Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting an array

 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why is the exception thrown?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code snippet should help

 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're saying that since runtime class of Number[] na in the code is [Ljava.lang.Number we cannot cast it to Integer[]. Which is corrected by this code:


But what about following code:

Since the runtime class of oa is [Ljava.lang.String; shouldnt we be able to cast it to String[] ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsh Pensi wrote:So you're saying that since runtime class of Number[] na in the code is [Ljava.lang.Number we cannot cast it to Integer[]. Which is corrected by this code:


But what about following code:

Since the runtime class of oa is [Ljava.lang.String; shouldnt we be able to cast it to String[] ?


I am able to do it without a RuntimeException



Outputs 3. Do you see a different result ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JDK_1_6_06 under Win XP 64 bit
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. I guess my RAD is just playing with me. It is giving Exception on this line if I have more code below if not its running fine.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Harsh,

This issue does not look me like of RAD region being I executed the code using the command line/eclipse in java 5 and I also got the class cast exception at run time.


I guess, the root cause is something else. Can it be running in some of our friends machine just because they have java 6 rather than java5. Btw if you tweak the code a bit number to integer casting works fine. The below code works fine




Regards
Salil Verma
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the code below



The code given above will compiles but throws runtime exception but if you remove comment on line number 5 then the above code will compiles and runs fine.
after line number 5 actual super class reference refer to sub class object so casting to Integer[] successfull at runtime but when super class reference refer to object of itself (that is commenting line number 5) then casting at line number 6 throws exception at runtime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic