Home >Java >javaTutorial >What does i-- mean in java?

What does i-- mean in java?

下次还敢
下次还敢Original
2024-04-29 02:45:23957browse

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.

What does i-- mean in java?

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:

  • Decreasing a loop variable in a traversal loop
  • When updating a counter
  • When deleting an element from a list or array

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does i+ mean in javaNext article:What does i+ mean in java