What is the result of the following code?
The following code wants to test whether the three variables i, j and k have the same value.
int i; int j; int k; boolean b; i = j = k = 1; // Assign 1 to i, j, and k b = (i == j == k);
b = (i == j == k); // A compile-time error The expression (i == j == k) resulted in an error.
The expression (i == j == k) is evaluated as follows:
First, i == j is evaluated in expression i == j == k. Since both i and j have the same value, which is 1, the expression i == j returns true.
The first step reduced the expression i == j == k to true == k.
After that the operands of == operator are working on boolean and int types. You cannot mix boolean and numeric types operands with the equality operator.