Yes, the compiler has access to more precise information, but the point
is that it should not use it. As it's optimising away code by doing the
calculation at compile time instead of at runtime, the result should be
the same as it would be at runtime.
I've just checked the spec, and this is actually the specified
behaviour (7.18):
<quote>
The compile-time evaluation of constant expressions uses the same
rules as run-time evaluation of non-constant expressions, except that
where run-time evaluation would have thrown an exception, compile-time
evaluation causes a compile-time error to occur.
</quote>
I'm pretty sure I've seen this not being the case, however. Will try
to reproduce when I get the time.
In this example it's rather obvious that the compiler will do the
calculation at compile time, but in other situations it may not be that
obvious, for example when one of the operands is a class member that may
or may not be cosntant. You have to be able to trust that the result
will be the same regardless if the calculation is done at compile time
or runtime.
There are lots of places where you won't get the same results
depending on how the compilation is achieved. Even putting extra
statements *after* the calculation can affect the result of the
calculation, based on whether the JIT can just use an 80 bit register
for the result, etc. Just a caveat, really.
Jon