Java Arithmetic Operator Question 6

Question

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

Note

Java Assignment Operators:

OperatorName Example Equivalent
+= Addition assignmenti += 8 i = i + 8
-= Subtraction assignment i -= 8 i = i - 8
*= Multiplication assignment i *= 8 i = i * 8
/= Division assignmenti /= 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); 



PreviousNext

Related