Guys, Is there any general explanation about what compiler looks for against JVM(at run time). In many of the questions, we tend to know where its going wrong, but can't predict whether its a compiler or runtime error.
If the syntax says, 2D can't be assigned to 1D array, why didn't throw a compiler error?
It is matter of run time. At compile time it is not known, to what dimension of array the ref variable o1 is pointing to. Arrays in java are simply Object with capital O. Object ref variable can refer to array of any dimension on the heap. At runtime o1 refers to 2D array, the JVM comes to know the wrong assignment(casting). Compiler is innocent of all those things.
In short, compiler can't find out the runtime behavior. It is what JVM is for.
Your question is fundamentally no different from -
"Why cannot the compiler evaluate the expression
at compile time and fail because the expression evaluates to infinity?"
Basically, the compiler parses source code, ensures syntactical correctness against language grammar, etc. It does not actually evaluate the expressions.
In your program, your cast at line 7 subverts the type-check, and fools the compiler into believing that the types on both sides of the '=' are the same. You pay the price of cheating the compiler as a run-time exception.
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
If A is a superclass of B, a = b; is valid at compilation time.
If B is a superclass of A, a = b; is not valid at compilation time. However, a = (a) b is valid as it defers the check to runtime. For objects of type lower than A having reference of type B can be safely cast to type A.
If A and B do not belong to the same hierarchy, both a = b; and a = (a) b; are invalid at compilation time. [ July 21, 2007: Message edited by: Abhinav Srivastava ]
Thanks all for replying. There are still some area thats not very clear. Replies were mostly example based; there are other scenarios where you would want to predict whether its compiler or runtime errors.
Sun has the final word on java. So Sun must have explained how compiler or JVM should behave somewhere. Does anyone know where I can find those specification.
Come with any scenario that pinches your mind. Compiler does not have dynamic behavior in the sense, it can't know for sure the details about the run time of the program.
One more example I can tell you, that is downcasting. It is ok if you downcast with explicit casting. Compiler is happy with that but at rum time when JVM sees this is not OK, it throws ClassCastException.
See the code again:
If we change Line #1 to Animal a1 = new Dog(); then there wont be ClassCastException at Line #2. Because Dog reference variable d1 can hold object of the Dog obviously.
I shown you some scenarios where you can determine, what may go wrong, just seeing at code. Other situation may depend upon runtime circumstances of the program, for instance IOException, FileNotFoundException, NotSerializable exception etc. But you know the probability of exception so you are forced to handle the checked exceptions by the compiler whereas you are free in the case of unchecked exceptions.
I think your main problem is, that the samples are too small. You see the code and think that the error is obvious. Look at the following sample and ask yourself, how the compiler could know that worker.doStuff() returns a Cat.
He baked a muffin that stole my car! And this tiny ad: