• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does this code not throw ClassCastException or NullPointerException?

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I have this code,



If I change my code in line 1 to Object o="abc"; then, ClassCastException is thrown. Can any one explain me why is this not throwing the same/NullPointerException, exception in original code? What is that different in assigning a null and "abc" to the Object o?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can cast a null to any type! It is just the way JLS is designed! If you think practically, any Object type in Java can be null!



 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, @Joe Harry the value null is associated here to an Object which is a super class of String!!! So why is that not caught at runtime and throw classCastException? as, in java direct casting from parent to child throws classCastException!!! Though its clear from your post, that NullPointerException is not at all an option here.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can cast null from any type to any type which is why you did not see a ClassCastException! At Runtime, there is no actual object in the heap to downcast to, so you don't see any exception.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much @Joe Harry
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:If I change my code in line 1 to Object o="abc"; then, ClassCastException is thrown.

Seriously When I make that change, the program still runs fine, without printing anything. And that's completely normal too: reference variable o is referring to a string "abc", so you can definitely cast it to the String type. It would have been a whole other story if you assigned 1_000_000 to reference variable o.

So there is no difference in assigning null or "abc" to the reference variable o...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:as, in java direct casting from parent to child throws classCastException!

That's not (always) true! It all depends on the type of object the reference variable is referring to.

Some examples to illustrate the above statement:


Hope it helps!
Kind regards,
Roel
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, @Roel De Nijs, I was wrong in my previous post. Your post helped me a lot in understanding the concept, not only Object-String but in general casting in user defined class as well. If you have not thrown light on my mistake, it would have led me, serious misunderstanding about the casting.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:If you have not thrown light on my mistake, it would have led me, serious misunderstanding about the casting.

Glad to hear I could help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic