• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Type Casting in Java

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why below code is valid it even do not throw any runtime exception??
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The null reference value may be cast to any reference type -- as all reference variables may have the value of null.

Henry
 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Henry already explained , I also want to give my opinion. Here you probably expected NullPointerException or ClassCastException. But that doesn't occur. NullPointerException occurs when you call a method/variable on reference variable which is  null. So here  you are not calling anything on references , so no chance of  NullPointerException. ClassCastException occurs when you type cast super class reference to sub class reference type if super class reference variable is pointing to super class object. Here super class reference variable is not  pointing to any object. So no chance of ClassCastException. Overall you don't get any exceptions here.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic