Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Reference variable casting doubt

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


If above program can compile and run successfully then what's problem in given below thanks

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your second case, you're casting an Animal reference to a Dog reference. Now, that might work, so it will compile OK. But at run-time the JVM will look at the actual type of the object, which is Animal, and will try and cast that to a Dog. That's not possible, because an Animal is-not-a Dog, so you get a ClassCastException.

In the first example you're preceding the cast with an instanceof check. Which means that for those cases where you would get a ClassCastException (the first and last elements in the array), it doesn't try and cast. It only tries it for the case where the cast is safe - when the object actually is a Dog.

You should always check instanceof before casting an object like that unless you know you can guarantee (from your understanding of the rest of the code) that it is safe.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:In your second case, you're casting an Animal reference to a Dog reference. Now, that might work, so it will compile OK. But at run-time the JVM will look at the actual type of the object, which is Animal, and will try and cast that to a Dog. That's not possible, because an Animal is-not-a Dog, so you get a ClassCastException.

In the first example you're preceding the cast with an instanceof check. Which means that for those cases where you would get a ClassCastException (the first and last elements in the array), it doesn't try and cast. It only tries it for the case where the cast is safe - when the object actually is a Dog.

You should always check instanceof before casting an object like that unless you know you can guarantee (from your understanding of the rest of the code) that it is safe.



Thanks Matthew:)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic