## 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.

## 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.

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

### 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.减法运算符

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 (*) is used to multiply two values or variables:

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

### 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.

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.

### 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 (%):

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

## 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.

### 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.单项减法运算符

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 (!) is also known as the “NOT” operator. We can use it to invert the value of a boolean variable or value:

``````boolean aTrue = true;
boolean bFalse = !aTrue;``````

### 3.4.增量运算符

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

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

### 3.5.递减运算法则

The decrement operator (–) does the opposite of the increment operator. It decreases the value of a variable by 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.

## 4.关系操作符

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

### 4.1.等于 “运算符

We use the “equal to” operator (==) to compare the values on both sides. If they’re equal, the operation returns 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.

### 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.大于 “运算符

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:

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

### 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:

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

### 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:

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

### 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:

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

## 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.

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

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

### 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.逻辑OR运算符

Unlike the ‘&&‘ operator, the logical OR operator (||) returns true if at least one operand is 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.

## 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 ( condition ) {
expression1
} else {
expression2
}``````

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

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.位操作符和位移操作符

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

### 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.位数OR运算符

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

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

### 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 (~) 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.左移操作符

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.

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

``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 (>>) 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.

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:

``````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 (>>>) works in a similar way as the ‘>>’ operator. The only difference is that after a shift, the leftmost bit is set to 0.

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

``````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.

## 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:

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

## 9.赋值操作者

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

### 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:

``int seven = 7;``

### 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“.

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

``````// 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.结语

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

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