File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compiler vs. Runtime Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compiler vs. Runtime Error" Watch "Compiler vs. Runtime Error" New topic

Compiler vs. Runtime Error

juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Is there anything like a "Rule of Thumb" or
does anyone know of an easier way to learn
which errors would result in a Runtime Error,
and which would result in a Compiler Error?
For example, using the dot-operator on null
results in a Runtime Error; why wouldn't want
the compiler take care of it?
Thanx for answering,

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The compiler will report any errors it can find, but it's not all that smart. It never knows the value that any variable contains, so it will always allow you to dereference a variable (as long as the compiler is sure the variable has been initialized to some value.)
Similarly, it can check whether a type cast is plausible, but it can't check whether the cast will definitely succeed or not, so casts fail at runtime unless the compiler can prove that two types definitely can't be interconverted.

[Jess in Action][AskingGoodQuestions]
It is sorta covered in the JavaRanch Style Guide.
subject: Compiler vs. Runtime Error
It's not a secret anymore!