• 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

Casting an array

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


Why is the exception thrown?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code snippet should help

 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic