Java Operators – Java操作者

最后修改: 2022年 3月 26日

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

1. Overview

1.概述

Operators are a fundamental building block of any programming language. We use operators to perform operations on values and variables.

操作符是任何编程语言的一个基本构件。我们使用运算符来对值和变量进行操作。

Java provides many groups of operators. They are categorized by their functionalities.

Java提供了许多组操作符。它们按其功能进行分类。

In this tutorial, we’ll walk through all Java operators to understand their functionalities and how to use them.

在本教程中,我们将通过所有的Java操作符来了解它们的功能以及如何使用它们。

2. Arithmetic Operators

2.算术运算符

We use arithmetic operators to perform simple mathematical operations. We should note that arithmetic operators only work with primitive number types and their boxed types, such as int and Integer.

我们使用算术运算符来进行简单的数学运算。我们应该注意,算术运算符只适用于原始数字类型和它们的盒式类型,如intInteger

Next, let’s see what operators we have in the arithmetic operator group.

接下来,让我们看看在算术运算组中我们有哪些运算符。

2.1. The Addition Operator

2.1.加法运算符

The addition operator (+) allows us to add two values or concatenate two strings:

加法运算符(+)允许我们将两个值相加或将两个字符串串联起来。

int ten = 5 + 5;
String youAndMe = "You " + "and" + " Me";

2.2. The Subtraction Operator

2.2.减法运算符

Usually, we use the subtraction operator (-) to subtract one value from another:

通常情况下,我们使用减法运算符(-)来从另一个数值中减去一个数值。

int five = 10 - 5;
int negative80 = 20 - 100;

2.3. The Multiplication Operator

2.3.乘法运算符

The multiplication operator (*) is used to multiply two values or variables:

乘法运算符(*)用于两个数值或变量的相乘。

int hundred = 20 * 5;
int fifteen = -5 * -3;

2.4. The Division Operator

2.4.分部操作员

The division operator (/) allows us to divide the left-hand value by the right-hand one:

除法运算符(/)允许我们用左边的数值除以右边的数值。

int four = 20 / 5;
int seven = 15 / 2;

When we use the division operator on two integer values (byte, short, int, and long), we should note that the result is the quotient value. The remainder is not included.

当我们对两个整数值(byte, short, int, andlong)使用除法运算时,我们应该注意结果是商值。剩余部分不包括在内

As the example above shows, if we calculate 15 / 2, the quotient is 7, and the remainder is 1. Therefore, we have 15 / 2 = 7.

正如上面的例子所示,如果我们计算15 / 2,商是7,,余数是1。因此,我们有15 / 2 = 7

2.5. The Modulo Operator

2.5.摩尔运算符

We can get the quotient using the division operator. However, if we just want to get the remainder of a division calculation, we can use the modulo operator (%):

我们可以用除法运算符得到商。然而,如果我们只想得到除法计算的余数,我们可以使用modulo运算符(%)。

int one = 15 % 2;
int zero = 10 % 5;

3. Unary Operators

3.单元运算符

As the name implies, unary operators only require one single operand. For example, we usually use unary operators to increment, decrement, or negate a variable or value.

顾名思义,单项运算符只需要一个操作数。例如,我们通常使用单项运算符来增加、减少或否定一个变量或值。

Now, let’s see the details of unary operators in Java.

现在,让我们看看Java中单项运算符的细节。

3.1. The Unary Plus Operator

3.1.单数加运算符

The unary plus operator (+) indicates a positive value. If the number is positive, we can omit the ‘+’ operator:

单元加号运算符(+)表示正值。如果数字是正数,我们可以省略’+’运算符。

int five = +5; // same as: int five = 5

3.2. The Unary Minus Operator

3.2.单项减法运算符

Opposite to the unary plus operator, the unary minus operator (-) negates a value or an expression:

与单数加号运算符相反,单数减号运算符(-)否定一个值或一个表达式。

int negativeFive = -5;
int eighty = -(20 - 100);

3.3. The Logical Complement Operator

3.3.逻辑补数运算符

The logical complement operator (!) is also known as the “NOT” operator. We can use it to invert the value of a boolean variable or value:

逻辑补码运算符(!)也被称为“NOT “运算符。我们可以用它来反转boolean变量或值的值。

boolean aTrue = true;
boolean bFalse = !aTrue;

3.4. The Increment Operator

3.4.增量运算符

The increment operator (++) allows us to increase the value of a variable by 1:

增量运算符(++)允许我们将一个变量的值增加1。

int number = 5;
number++; // number = 6

3.5. The Decrement Opeartor

3.5.递减运算法则

The decrement operator (–) does the opposite of the increment operator. It decreases the value of a variable by 1:

递减运算符(-)的作用与递增运算符相反。它将一个变量的值减少1。

int number = 5;
number--; // number = 4

We should keep in mind that the increment and decrement operators can only be used on a variable. For example, “int a = 5; a++;” is fine. However, the expression “5++” won’t be compiled.

我们应该记住,增量和减量运算符只能用于一个变量。例如,”int a = 5; a++;“是可以的。然而,表达式”5++“将不会被编译。

4. Relational Operators

4.关系操作符

Relational operators can be called “comparison operators” as well. Basically, we use these operators to compare two values or variables.

关系运算符也可以被称为 “比较运算符”。基本上,我们使用这些运算符来比较两个值或变量。

4.1. The “Equal To” Operator

4.1.等于 “运算符

We use the “equal to” operator (==) to compare the values on both sides. If they’re equal, the operation returns true:

我们使用 “等于 “运算符(==)来比较两边的值。如果它们相等,该操作返回true

int number1 = 5;
int number2 = 5;
boolean theyAreEqual = number1 == number2;

The “equal to” operator is pretty straightforward. On the other hand, the Object class has provided the equals() method. As the Object class is the superclass of all Java classes, all Java objects can use the equals() method to compare each other.

“等于 “运算符是非常直接的。另一方面,Object类提供了equals()方法。由于Object类是所有Java类的超类,所有Java对象都可以使用equals()方法来相互比较。

When we want to compare two objects – for instance, when we compare Long objects or compare Strings – we should choose between the comparison method from the equals() method and that of the “equal to” operator wisely.

当我们想比较两个对象时 – 例如。当我们比较Long对象比较Strings时,我们应该明智地选择equals() 方法的比较方法和 “等于 “操作符的方法。

4.2. The “Not Equal To” Operator

4.2.不等于 “操作符

The “not equal to” operator (!=) does the opposite of the ‘==’ operator. If the values on both sides are not equal, the operation returns true:

“不等于 “运算符(!=)的作用与’==’运算符相反。如果两边的值不相等,该操作返回true

int number1 = 5;
int number2 = 7;
boolean notEqual = number1 != number2;

4.3. The “Greater Than” Operator

4.3.大于 “运算符

When we compare two values with the “greater than” operator (>), it returns true if the value on the left-hand side is greater than the value on the right-hand side:

当我们用 “大于 “运算符(>)比较两个值时,如果左边的值大于右边的值,它将返回true

int number1 = 7;
int number2 = 5;
boolean greater = number1 > number2;

4.4. The “Greater Than or Equal To” Operator

4.4.大于或等于 “操作符

The “greater than or equal to” operator (>=) compares the values on both sides and returns true if the left-hand side operand is greater than or equal to the right-hand side operand:

大于或等于 “运算符(>=)比较两边的值,如果左边的操作数大于或等于右边的操作数,则返回true

int number1 = 7;
int number2 = 5;
boolean greaterThanOrEqualTo = number1 >= number2;
number1 = 5;
greaterThanOrEqualTo = number1 >= number2;

4.5. The “Less Than” Operator

4.5.小于 “运算符

The “less than” operator (<) compares two values on both sides and returns true if the value on the left-hand side is less than the value on the right-hand side:

小于 “运算符(<)比较两边的值,如果左边的值小于右边的值,则返回true

int number1 = 4;
int number2 = 5;
boolean lessThan = number1 < number2;

4.6. The “Less Than or Equal To” Operator

4.6.小于或等于 “操作符

Similarly, the “less than or equal to” operator (<=) compares the values on both sides and returns true if the left-hand side operand is less than or equal to the right-hand side:

同样,”小于或等于 “运算符(<=)比较两边的值,如果左边的操作数小于或等于右边的操作数,则返回true

int number1 = 4;
int number2 = 5;
boolean lessThanOrEqualTo = number1 <= number2;
number1 = 5;
lessThanOrEqualTo = number1 <= number2;

5. Logical Operators

5.逻辑运算符

We have two logical operators in Java: the logical AND and OR operators. Basically, their function is pretty similar to the AND gate and the OR gate in digital electronics.

我们在 Java 中有两个逻辑运算符:逻辑 AND 和 OR 运算符。基本上,它们的功能与数字电子中的AND 门OR 门相当相似。

Usually, we use a logical operator with two operands, which are variables or expressions that can be evaluated as boolean.

通常,我们使用一个带有两个操作数的逻辑运算符,这些操作数是可以被评估为boolean的变量或表达式。

Next, let’s take a closer look at them.

接下来,让我们仔细看看它们。

5.1. The Logical AND Operator

5.1.逻辑与运算符

The logical AND operator (&&) returns true only if both operands are true:

逻辑和运算符(&&)只有在两个操作数都是时才会返回

int number1 = 7;
int number2 = 5;
boolean resultTrue = (number1 > 0) && (number1 > number2);

5.2. The Logical OR Operator

5.2.逻辑OR运算符

Unlike the ‘&&‘ operator, the logical OR operator (||) returns true if at least one operand is true:

与’&&‘运算符不同,逻辑OR运算符(||)如果至少有一个操作数是true,则返回true

int number1 = 7;
int number2 = 5;
boolean resultTrue = (number1 > 100) || (number1 > number2);

We should note that the logical OR operator has the short-circuiting effect: It returns true as soon as one of the operands is evaluated as true, without evaluating the remaining operands.

我们应该注意,逻辑OR运算符具有短路效应只要其中一个操作数被评估为true,它就会返回true,而不会评估其余的操作数。

6. Ternary Operator

6.三元运算符

A ternary operator is a short form of the if-then-else statement. It has the name ternary as it has three operands. First, let’s have a look at the standard if-then-else statement syntax:

三元运算符if-then-else语句的一种简称。它的名字是三元,因为它有三个操作数。首先,让我们看一下标准的if-then-else语句语法。

if ( condition ) {
    expression1
} else {
    expression2
}

We can convert the above if-then-else statement into a compact version using the ternary operator:

我们可以使用三元运算符将上述if-then-else语句转换为一个紧凑的版本。

Let’s look at its syntax:

我们来看看它的语法。

condition ? expression1 : expression2

Next, let’s understand how the ternary operator works through a simple example:

接下来,让我们通过一个简单的例子来了解三元运算符的工作原理。

int number = 100;
String greaterThan50 = number > 50 ? "The number is greater than 50" : "The number is NOT greater than 50";

7. Bitwise and Bit Shift Operators

7.位操作符和位移操作符

As the article “Java bitwise operators” covers the details of bitwise and bit shift operators, we’ll briefly summarize these operators in this tutorial.

由于”Java bitwise运算符“一文涵盖了bitwise和bit shift运算符的细节,我们将在本教程中简要地总结这些运算符。

7.1. The Bitwise AND Operator

7.1.位和运算符

The bitwise AND operator (&) returns the bit-by-bit AND of input values:

位数和运算符(&)返回输入值的逐位和。

int number1 = 12;
int number2 = 14;
int twelve = number1 & number2; // 00001100 & 00001110 = 00001100 = 12

7.2. The Bitwise OR Operator

7.2.位数OR运算符

The bitwise OR operator (|) returns the bit-by-bit OR of input values:

位数OR运算符(|)返回输入值的逐位OR。

int number1 = 12;
int number2 = 14;
int fourteen = number1 | number2; // 00001100 | 00001110 = 00001110 = 14

7.3. The Bitwise XOR Operator

7.3.比特XOR运算符

The bitwise XOR (exclusive OR) operator (^) returns the bit-by-bit XOR of input values:

bitwise XOR(排他性OR)运算符(^)返回输入值的逐位XOR。

int number1 = 12;
int number2 = 14;
int two = number1 ^ number2; // 00001100 ^ 00001110 = 00000010 = 2

7.4. The Bitwise Complement Operator

7.4.位补运算符

The bitwise complement operator (~) is a unary operator. It returns the value’s complement representation, which inverts all bits from the input value:

位补运算符(~)是一个单数运算符。它返回值的补码表示法,它将输入值的所有位都倒置。

int number = 12;
int negative13 = ~number; // ~00001100 = 11110011 = -13

7.5. The Left Shift Operator

7.5.左移操作符

Shift operators shift the bits to the left or right by the given number of times.

移位运算符将比特向左或向右移位给定的次数。

The left shift operator (<<) shifts the bits to the left by the number of times defined by the right-hand side operand. After the left shift, the empty space in the right is filled with 0.

左移运算符(<<)将比特向左移,移的次数由右边的操作数定义。左移后,右边的空位被填充为0。

Next, let’s left shift the number 12 twice:

接下来,让我们把数字12左移两次。

int fourtyeight = 12 << 2; // 00001100 << 2 = 00110000 = 48

n << x has the same effect of multiplying the number n with x power of two.

n << x的效果与数字n乘以x的2次方相同。

7.6. The Signed Right Shift Operator

7.6.有符号的右移运算符

The signed right shift operator (>>) shifts the bits to the right by the number of times defined by the right-hand side operand and fills 0 on voids left as a result.

有符号的右移操作符(>>)将比特向右移动,移动的次数由右侧操作数定义,并在留下的空位上填入0,作为结果。

We should note that the leftmost position after the shifting depends on the sign extension.

我们应该注意到,移位后的最左位置取决于符号扩展

Next, let’s do “signed right shift” twice on the numbers 12 and -12 to see the difference:

接下来,让我们对数字12和-12做两次 “有符号的右移”,看看有什么不同。

int three = 12 >> 2; // 00001100 >> 2 = 00000011 = 3
int negativeThree = -12 >> 2; // 11110100 >> 2 = 11111101 = -3

As the second example above shows, if the number is negative, the leftmost position after each shift will be set by the sign extension.

如上面第二个例子所示,如果数字是负数,每次移位后最左边的位置将由符号扩展设置。

n >> x has the same effect of dividing the number n by x power of two.

n >> x的效果与数字n除以x的2次方相同。

7.7. The Unsigned Right Shift Operator

7.7.无符号右移操作符

The unsigned right shift operator (>>>) works in a similar way as the ‘>>’ operator. The only difference is that after a shift, the leftmost bit is set to 0.

无符号右移操作符(>>>)的工作方式与’>>’操作符类似。唯一的区别是,移位后,最左边的位被设置为0

Next, let’s unsigned right shift twice on the numbers 12 and -12 to see the difference:

接下来,让我们对数字12和-12进行无符号右移两次,看看有什么不同。

int three = 12 >>> 2; // 00001100 >> 2 = 00000011 = 3
int result = -12 >>> 2; // result = 1073741821 (11111111111111111111111111110100 >>> 2 = 00111111111111111111111111111101)

As we can see in the second example above, the >>> operator fills voids on the left with 0 irrespective of whether the number is positive or negative.

正如我们在上面的第二个例子中看到的,>>>运算符将左边的空隙填为0,而不管这个数字是正还是负

8. The “instanceof” Operator

8.”instanceof“操作符

Sometimes, when we have an object, we would like to test if it’s an instance of a given type. The “instanceof” operator can help us to do it:

有时,当我们有一个对象时,我们想测试它是否是一个特定类型的实例。”instanceof“操作符可以帮助我们做到这一点。

boolean resultTrue = Long.valueOf(20) instanceof Number;

9. Assignment Operators

9.赋值操作者

We use assignment operators to assign values to variables. Next, let’s see which assignment operators we can use in Java.

我们使用赋值运算符来给变量赋值。接下来,让我们看看在Java中我们可以使用哪些赋值运算符。

9.1. The Simple Assignment Operator

9.1.简单赋值运算符

The simple assignment operator (=) is a straightforward but important operator in Java. Actually, we’ve used it many times in previous examples. It assigns the value on its right to the operand on its left:

简单的赋值运算符(=)是Java中一个直接但重要的运算符。实际上,我们在以前的例子中已经多次使用过它。它把它右边的值分配给它左边的操作数。

int seven = 7;

9.2. Compound Assignments

9.2.化合物的分配

We’ve learned arithmetic operators. We can combine the arithmetic operators with the simple assignment operator to create compound assignments.

我们已经学习了算术运算符。我们可以将算术运算符与简单赋值运算符结合起来,创建复合赋值。

For example, we can write “a = a + 5” in a compound way: “a += 5“.

例如,我们可以把”a = a + 5“写成一个复合的方式。”a += 5“。

Finally, let’s walk through all supported compound assignments in Java through examples:

最后,让我们通过实例来了解Java中所有支持的复合赋值。

// Assuming all variables (a,b,c,d,e) have the initial value 10
a += 4; // a = 14, same as a = a + 4
b -= 4; // b = 6, same as b = b - 4
c *= 4; // c = 40, same as c = c * 4
d /= 4; // d = 2, same as d = d / 4
e %= 4; // e = 2, same as e = e % 4

10. Conclusion

10.结语

Java provides many groups of operators for different functionalities. In this article, we’ve passed through the operators in Java.

Java为不同的功能提供了许多组运算符。在这篇文章中,我们已经通过了Java中的运算符。