We would like to
What is the output of the following code?
public class Main { public static void main(String[] args) { int sum = 0; sum += 4.5; System.out.println(sum); } }
4
In Java, an augmented expression of the form x1 op= x2 is implemented as
x1 = casting in an augmented (T)(x1 op x2), where T is the type for x1.
Therefore, the following code is correct.
int sum = 0; sum += 4.5; // sum becomes 4 after this statement sum += 4.5 is equivalent to sum = (int)(sum + 4.5).