This should hopefully explain why.
<pre>
class PrimConvert {
double h = 0.0, k = 2.0;
int f = 2, g = 5;
public static void main (
String[] args) {
PrimConvert pc = new PrimConvert();
pc.h = 3+pc.f/pc.g+2;
System.out.println(pc.h);
pc.h = 3+pc.k/pc.g+2;
System.out.println(pc.h);
}
}
OUTPUT:
5.0
5.4
</pre>
promotion is done based on the operand types. In this case, the division operation has precedence. Both operands are integers. Hence, 2/5 will result in 0. The addition operations are done left to right so 3+0+2 results in 5.
Now, my second example uses a double in place of the integer in the division operation. So we have 2.0/5 which causes the compiler to promote 5 to a double. This results in 0.4. Doing addition left to right. 3+.4 will cause 3 to be promoted to 3.0. This results in 3.4. The last addition will be 3.4+2. Same scenario as before with the promotion. Final result is 5.4 and gets assigned to h which is a double already.