,

Left-to-Right Evaluation of Argument Lists – Basic Elements, Primitive Data Types, and Operators

Left-to-Right Evaluation of Argument Lists

In a method or constructor invocation, each argument expression in the argument list is fully evaluated before any argument expression to its right.

If evaluation of an argument expression does not complete normally, we cannot presume that any argument expression to its right has been evaluated.

We can use the add3() method at (4) in Example 2.1, which takes three arguments, to demonstrate the order in which the arguments in a method call are evaluated. The method call at (2)

Click here to view code image

add3(eval(i++, “, “), eval(i++, “, “), eval(i, “\n”));  // (2) Three arguments.

results in the following output, clearly indicating that the arguments were evaluated from left to right, before being passed to the method:

1, 2, 3
6

Note how the value of variable i changes successively from left to right as the first two arguments are evaluated.

2.7 The Simple Assignment Operator =

The assignment statement has the following syntax:

variable
 =
expression

which can be read as “the target, variable, gets the value of the source, expression.” The previous value of the target variable is overwritten by the assignment operator =.

The target variable and the source expression must be assignment compatible. The target variable must also have been declared. Since variables can store either primitive values or reference values, expression evaluates to either a primitive value or a reference value.

Assigning Primitive Values

The following examples illustrate assignment of primitive values:

Click here to view code image

int j, k;
j = 0b10;         // j gets the value 2.
j = 5;            // j gets the value 5. Previous value is overwritten.
k = j;            // k gets the value 5.

The assignment operator has the lowest precedence, so the expression on the right-hand side is evaluated before the assignment is done.

Click here to view code image int i;
i = 5;            // i gets the value 5.
i = i + 1;        // i gets the value 6. + has higher precedence than =.
i = 20 – i * 2;   // i gets the value 8: (20 – (i * 2))

Related Posts