• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting compile time vs runtime errors

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, the OCA book says that we should be able to
discern between compiler-time casting errors and those that will not occur until runtime and that throw a CastClassException

So, how do we do that? I mean how do I know if a casting error happens at runtime or compile time? ANy idea how and where to find out please?
thanks
 
Emanuel Frua
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand the concepts of polymorphism, inheritance and IS-A relationship?.
You should try casting (implicitly and explicitly) some combinations in your IDE and draw a map between classes.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't say I'm an expert no, but yes I do understand the difference, it's just that I have no idea when a compilation vs runtime error would occur
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A compiler error happens when a compiler can determine from your code that in no possible circumstance the cast is valid.

For instance, it's never possible to cast a String to an Integer, because the two are not in the same type hierarchy. It is possible to cast a Number to an Integer though, because an Integer is-a Number. If at runtime the actual type turns out to be a Double instead of an Integer, you will get a ClassCastException.

You may cast a non-final class to ANY interface, because even if the class doesn't implement the interface, it's actual type may be a sub-class that does implement the interface. If the actual type turns out not to implement the interface, you will get a ClassCastException. For the same reason, you may cast an interface to ANY non-final class.

Final class may not be cast to any interface, only to interfaces that it actually implements. Interfaces may be cast to non-final classes, but only if those classes implement the interfaces.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! jason,

when a compiler compiles a code it checks for the Syntax error and some kind of basic logic checks such as putting a difference b/w reachable and unreachable statements,class found or not,file name checks,precision checks(shouldn't be loosed) etc..
but it doesnot checks for your deep logic of your program,it simply converts your code(After a type erasure-->leaving no type related information) into byte code for the jvm where the logic check will happen.
even you are doing a calculation which gives you a not defined thing is interpreted at JVM level.

now consider the code below:-



In the above code you will not get any compile time error but ClassCastException during runtime because compiler knows that Person can be casted to Emanuel
rather than doing a check for actually it is or not because it is the work of JVM.

Hope this helps!

Thankyou,
kind Regards,
praveen.
 
Emanuel Frua
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you've got good answers and a live example  . If you still in doubt try to focus on the reference type for compile-time errors and the object type for runtime exceptions.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic