What is the output of the following code?
public class Main { public static void main(String[] args) { double x = 10; x /= 10 + 5 * 2; System.out.println("x: " + x); } }
x: 0.5
Java Assignment Operators:
Operator | Name | Example | Equivalent |
---|---|---|---|
+= | Addition assignment | i += 8 | i = i + 8 |
-= | Subtraction assignment | i -= 8 | i = i - 8 |
*= | Multiplication assignment | i *= 8 | i = i * 8 |
/= | Division assignment | i /= 8 | i = i / 8 |
%= | Remainder assignment | i %= 8 | i = i % 8 |
The assignment operator is performed last after all the other operators in the expression are evaluated.
For example,
x /= 10 + 5 * 2; is x = x / (10 + 5 * 2);