Witscale Test Center

9.6 Assignment operators > 9.6.1 Assignment returns a value


9.6.1 Assignment returns a value

Assignment operators are evaluated differently from all the other operators in Java. They are right associative which means that the right-hand side of assignment operator is evaluated first and then its value is stored in the variable specified on the left side. Despite this unique behavior, the assignment symbols are still considered as operators. Like all other Java operators, they too return value. Therefore, you can use the assignment expression as an operand to some other operator. Following example shows how the assignment expression is used as an operand to + operator:

 

int a;

int b = (a = 8) + 6;

System.out.println("a= " + a + " b= " + b);  //a= 8 b= 14 is printed

 

The assignment a = 8 returns a value 8 and then it acts as first operand in addition 8 + 6. The result 14 is assigned to b.