A Guide to Increment and Decrement Unary Operators in Java – Java中的增量和减量单项操作符指南

最后修改: 2019年 8月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll briefly discuss the increment and decrement unary operators in Java.

在本教程中,我们将简要地讨论Java中的增量和减量单项运算符。

We’ll start by looking at the syntax followed by the usage.

我们先看一下语法,然后再看一下用法。

2. Increment and Decrement Operations in Java

2.Java中的增量和减量操作

In Java, the increment unary operator increases the value of the variable by one while the decrement unary operator decreases the value of the variable by one.

在Java中,增量单数运算符将变量的值增加1,而减量单数运算符将变量的值减少1。

Both update the value of the operand to its new value.

两者都更新操作数的值为其新值。

The operand required should be a variable that is not constant, as we wouldn’t be able to modify its value. Furthermore, the operand can’t be an expression because we cannot update them.

要求的操作数应该是一个非常量的变量,因为我们无法修改其值。此外,操作数不能是一个表达式,因为我们不能更新它们。

The increment and decrement unary operators have two forms, which are, prefix and postfix.

递增和递减的单数运算符有两种形式,即前缀和后缀。

3. Pre-Increment and Pre-Decrement Unary Operators

3.预增和预减的单项操作符

In the prefix form, the increment and decrement unary operators appear before the operand.

在前缀形式中,增量和减量单项运算符出现在操作数之前。

While using the prefix form, we first update the value of the operand and then we use the new value in the expression.

在使用前缀形式时,我们首先更新操作数的值,然后在表达式中使用新的值。

First, let’s look at a code snippet using the pre-increment unary operator:

首先,让我们看看一个使用预增单数运算符的代码片段。

int operand = 1;
++operand; // operand = 2
int number = ++operand; // operand = 3, number = 3

Next, let’s have a look at the code snippet using the pre-decrement one:

接下来,让我们看一下使用预减法的代码片断。

int operand = 2;
--operand; // operand = 1
int number = --operand; // operand = 0, number = 0

As we see, the prefix operators change the value of the operand first, and then the rest of the expression gets evaluated. This can easily lead to confusion if embedded in a complex expression. It’s recommended we use them on their own line rather than in larger expressions.

正如我们所看到的,前缀运算符首先改变操作数的值,然后表达式的其余部分被评估。如果嵌入复杂的表达式中,这很容易导致混乱。建议我们在自己的行中使用它们,而不是在较大的表达式中。

4. Post-Increment and Post-Decrement Unary Operators

4.后增量和后减量的单项操作符

In the postfix form, the operator appears after the operand.

在后缀形式中,运算符出现在操作数之后。

While using the postfix form, we first use the value of the operand in the expression and then update it.

在使用后缀形式时,我们首先使用表达式中操作数的值,然后更新它。

Let’s look at a sample code snippet using the post-increment operator:

让我们看看一个使用后置增量运算符的示例代码片断。

int operand = 1;
operand++; // operand = 2
int number = operand++; // operand = 3, number = 2

Also, let’s have a look at the post-decrement one:

另外,让我们看一下降级后的那个。

int operand = 2;
operand--; //operand = 1
int number = operand--; // operand = 0, number 1

Similarly, post-increment and post-decrement unary operators should be on their own line rather than including them in larger expressions.

同样,增量后和减量后的单数运算符应该在自己的行上,而不是把它们包含在更大的表达式中。

5. Conclusion

5.总结

In this quick tutorial, we learned about the increment and decrement unary operators in Java. Moreover, we looked at their two forms: prefix and postfix. Finally, we looked at its syntax and sample code snippets.

在这个快速教程中,我们了解了Java中的增量和减量单选运算符。此外,我们看了它们的两种形式:前缀和后缀。最后,我们看了它的语法和示例代码片断。

The full source code of our examples here is, as always, over on GitHub.

我们这里的例子的完整源代码一如既往地在GitHub上