Java precedence
The operator is not evaluated at first. Order:
- Evaluate LHS (
x++
) - Result is the original valuex
thenx
incremented - Estimate RHS (
10
) - Result 10 - Compare LHS and RHS Results
Here's some code demonstrating that:
public class Test {
static int x = 9;
public static void main(String[] args) {
boolean result = x++ >= showXAndReturn10();
System.out.println(result); // False
}
private static int showXAndReturn10() {
System.out.println(x); // 10
return 10;
}
}
This prints 10
, then false
, because at the time of the evaluation the RHS has x
been incremented ... but the operator >=
still evaluates 9 >= 10
as the result of the expression x++
- this is the original value x
, not the incremental one.
If you want to get the result after incrementing, use ++x
.
source to share
The relation operator is not evaluated until incremented.
The operands of the relational operator ( x++
and 10
) are processed first .
However, the score x++
increments x
but returns the original value x
, so even if the increment has already taken place, the value passed to the relational operator is the original value x
.
source to share
Because this is how "++, -" works. If it comes after the variable, the old value is used, then the value is increased, and if it comes before the variable, then the increment occurs first, then the new value is used. So, if you want to use a variable after increasing its value and before checking it, use (++ x> = 10) or increase it without reference, and then check it like:
int x = 0;
x++;
if(x >= 10) {...}
source to share
Unary Operators
However, you put unary operators in an expression, the following table shows its usage.
+----------+-------------------+------------+-----------------------------------------------------------------------------------+
| Operator | Name | Expression | Description |
+----------+-------------------+------------+-----------------------------------------------------------------------------------+
| ++ | prefix increment | ++a | Increment "a" by 1, then use the new value of "a" in the residing expression. |
| ++ | postfix increment | a++ | Use the current value of "a" in the residing expression, then increment "a" by 1. |
| -- | prefix decrement | --b | Decrement "b" by 1, then use the new value of "b" in the residing expression. |
| -- | postfix decrement | b-- | Use the current value of "b" in the residing expression, then decrement "b" by 1. |
+----------+-------------------+------------+-----------------------------------------------------------------------------------+
Then consider the following java program:
public class UnaryOperators {
public static void main(String args[]) {
int n;
// postfix unary operators
n = 10;
System.out.println(n); // prints 10
System.out.println(n++); // prints 10, then increment by 1
System.out.println(n); // prints 11
n = 10;
System.out.println(n); // prints 10
System.out.println(n--); // prints 10, then decrement by 1
System.out.println(n); // prints 9
// prefix unary operators
n = 10;
System.out.println(n); // prints 10
System.out.println(++n); // increment by 1, then prints 11
System.out.println(n); // prints 11
n = 10;
System.out.println(n); // prints 10
System.out.println(--n); // decrement by 1, then prints 9
System.out.println(n); // prints 9
}
}
source to share