Home >Java >javaTutorial >What does i-- mean in java?
i-- in Java is the post-decrement operator. First, the value of i is used as the operand, and then the value of i is decremented by 1. It is different from the pre-decrement operator (--i). It decrements the value of i before using it. Usually i-- is used when you need to decrement a variable before or after using it.
The meaning of i-- in Java
The meaning of i--
in Java Post-decrement operator, which first uses the value of variable i
as an operand, and then decrements the value of i
by 1
.
How it works
The following code demonstrates how the i--
operator works:
<code class="java">int i = 10; int j = i--; // j 等于 10,因为 i-- 先将 10 赋值给 j,然后再将 i 递减为 9</code>
Difference Prefix decrement (--i)
Prefix decrement operator (--i
) and post-decrement operator (i--
) is the order of operations:
--i
decrements the value of i
before using it as the operand, and then decrements the The value is assigned to i
. i--
First takes the value of i
as the operand and then decrements it. So, in the example above, --i
would make i
have the value 9
, while The value of j
is 8
:
<code class="java">int i = 10; int j = --i; // i 等于 9,因为 --i 先递减 i 为 9,然后再赋值给 j,导致 j 也等于 9</code>
When to use
Typically, when you need to decrement a variable before or after using it , i--
will be used.
For example:
The above is the detailed content of What does i-- mean in java?. For more information, please follow other related articles on the PHP Chinese website!