I'm having problems understanding what the JVM does at runtime and what the compiler does.
This code snippet is from K&B SCP&D for J2 book (p218):
This code won't compile because the case argument must be a compile time constant and apparently "b" is assigned "2" at runtime. In theory a more intelligent compiler would be able to substitute 2 for the final variable b into the bytecode.
Ok, fair enough, the compiler only does the minimum possible and leaves calculations and assignments for the runtime environment.
But then check this code (from same book, Self Test question 4.2):
Based on the first example I would have thought that "x-1" would be evaluated at runtime and consequently this code would cause a compile error. But no, all of a sudden the compiler isn't a complete doofus and can evaluate "x-1"
So my question is how do you work out what will be done at compile time and what is done at runtime, what rules of thumb are there?
Thanks for any insight.
(ps. I'm studying for the 310-035 exam, should I be posting questions like this here or in the Programmer Certification forum?)
In your first example, b is a blank final variable. It must be definitely assigned a value before it can be used. However, the compiler cannot determine the value at compile-time because the assignment is not made when the variable is declared.
Consider a situation where the blank final variable will be initialized by a value read from the user or from a file. The compiler can't possibly be expected to know the value.
In the second example, since x is a final and its value is known at compile-time, x-1 and x-2 can be computed at compile-time. [ September 11, 2006: Message edited by: Keith Lynn ]
posted 12 years ago
I hadn't considered the possibility that b could be initialised by a value read in from a file. I just assumed the compiler only had to look through the code to see what value is assigned to b, and if nothing is assigned spit the dummy. It's now starting to make more sense.
Originally posted by Martin hill: I hadn't considered the possibility that b could be initialised by a value read in from a file.
It doesn't even need to be that complicated. Just consider the following snippet:
Of course the compiler *could* have been written to notice that in your first example, the value actually is fixed. I guess they just decided that it would be too much trouble - and somewhere they *had* to draw the line...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Thanks Ilja and Vlado, your explanations have cleared up my confusion.
Vlado, that chapter 15 is one nasty chapter! You'd need a PhD in computing to begin to understand the section "Identify Matching Arity Methods Applicable by Subtyping" and a few sections that follow it. My dictionary doesn't even list the word "arity".