Originally posted by Peter Warde:
Matt
I'm not sure about this compile time constant. I don't think the code below is a compile time constant as the value of y would only be determined at runtime.
class Test {
int i;
Test(int y) {
i = y/2;
System.out.println(i);
}
public static void main( String args[]) {
Test t = new Test(1);
}
}
Ps does anybody know a good explanation of what is determined at compile time versus runtime
the way it works, with any arithmetic operations, and for that matter almost any statement that you can point out in a line of source, is there is an instruction for the specific operation that gets determined at compile time, based on the number of operands and their types, and then values are given to that instruction at runtime, which determines the result. so based on the types and numbers of operands you have in your source file, the compiler will determine a specific instruction that should be executed for that. in this case, it's an integer division, which truncates.