Java Operators
One of the most basic uses of computers is to perform mathematical operations. As a computer language, Java also provides a rich set of operators for manipulating variables. We can divide operators into the following groups:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Other Operators
Arithmetic Operators
Arithmetic operators are used in mathematical expressions, and their function is the same as in mathematics. The following table lists all the arithmetic operators.
In the examples in the table, assume integer variable A has value 10 and variable B has value 20:
| Operator | Description | Example |
|---|---|---|
| + | Addition - adds values on either side of the operator | A + B equals 30 |
| - | Subtraction - subtracts the right operand from the left operand | A - B equals -10 |
| * | Multiplication - multiplies values on either side of the operator | A * B equals 200 |
| / | Division - divides the left operand by the right operand | B / A equals 2 |
| % | Remainder - the remainder of dividing the left operand by the right operand | B%A equals 0 |
| ++ | Increment: increases the value of the operand by 1 | B++ or ++B equals 21 (see below for the difference) |
| -- | Decrement: decreases the value of the operand by 1 | B-- or --B equals 19 (see below for the difference) |
Example
The simple example program below demonstrates the arithmetic operators. Copy and paste the following Java program, save it as a Test.java file, then compile and run this program:
Example
Run example »
The result of compiling and running the above example is as follows:
a + b = 30 a - b = -10 a * b = 200 b / a = 2 b % a = 0 c % a = 5 a++ = 10 a-- = 11 d++ = 25 ++d = 27
Increment and Decrement Operators
1. Increment (++) and decrement (--) operatorsThey are special arithmetic operators. While arithmetic operators require two operands to perform an operation, the increment and decrement operators operate on one operand.
Example
The result is:
进行自增运算后的值等于4 进行自减运算后的值等于2
Explanation:
int b = ++a; The operation process is broken down as follows: a=a+1=4; b=a=4; the final result is b=4, a=4
int d = --c; The operation process is broken down as follows: c=c-1=2; d=c=2; the final result is d=2, c=2
2. Prefix increment and decrement (++a, --a):Perform the increment or decrement operation first, then evaluate the expression.
3. Postfix increment and decrement (a++, a--):Evaluate the expression first, then perform the increment or decrement operation. Example:
Example
The result is:
自增运算符前缀运算后a=6,x=12 自增运算符后缀运算后b=6,y=10
Relational Operators
The following table lists the relational operators supported by Java.
In the examples in the table, integer variable A has value 10 and variable B has value 20:
| Operator | Description | Example |
|---|---|---|
| == | Checks whether the values of two operands are equal; if they are equal, the condition is true. | (A == B) is false. |
| != | Checks whether the values of two operands are equal; if the values are not equal, the condition is true. | (A != B) is true. |
| > | Checks whether the value of the left operand is greater than the value of the right operand; if so, the condition is true. | (A > B) is false. |
| < | Checks whether the value of the left operand is less than the value of the right operand; if so, the condition is true. | (A < B) is true. |
| >= | Checks whether the value of the left operand is greater than or equal to the value of the right operand; if so, the condition is true. | (A >= B) is false. |
| <= | Checks whether the value of the left operand is less than or equal to the value of the right operand; if so, the condition is true. | (A <= B) is true. |
Example
The simple example program below demonstrates relational operators. Copy and paste the following Java program, save it as a Test.java file, then compile and run this program:
Test.java file code:
The result of compiling and running the above example is as follows:
a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = false
Bitwise Operators
Java defines bitwise operators, which are applied to integer type (int), long type (long), short type (short), character type (char), and byte type (byte), etc.
Bitwise operators act on all bits and perform operations bit by bit. Assume a = 60, b = 13; their binary representation will be as follows:
A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A | B = 0011 1101 A ^ B = 0011 0001 ~A= 1100 0011
The following table lists the basic operations of the bitwise operators, assuming integer variable A has value 60 and variable B has value 13:
| Operator | Description | Example |
|---|---|---|
| & | If the corresponding bits are both 1, the result is 1; otherwise, it is 0. | (A & B) yields 12, i.e., 0000 1100 |
| | | If the corresponding bits are both 0, the result is 0; otherwise, it is 1. | (A | B) yields 61, i.e., 0011 1101 |
| ^ | If the corresponding bit values are the same, the result is 0; otherwise, it is 1. | (A ^ B) yields 49, i.e., 0011 0001 |
| 〜 | The bitwise complement operator flips each bit of the operand, that is, 0 becomes 1 and 1 becomes 0. | (~A) yields -61, i.e., 1100 0011 |
| << | Bitwise left shift operator. The left operand is shifted left by the number of bits specified by the right operand. | A << 2 yields 240, i.e., 1111 0000 |
| >> | Bitwise right shift operator. The left operand is shifted right by the number of bits specified by the right operand. | A >> 2 yields 15, i.e., 1111 |
| >>> | Bitwise right shift with zero fill operator. The value of the left operand is shifted right by the number of bits specified by the right operand, and the vacated bits are filled with zeros. | A >>> 2 yields 15, i.e., 0000 1111 |
Example
The simple example program below demonstrates bitwise operators. Copy and paste the following Java program, save it as a Test.java file, then compile and run this program:
Test.java file code:
The result of compiling and running the above example is as follows:
a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 2 = 15 a >>> 2 = 15
Logical Operators
The following table lists the basic operations of the logical operators, assuming boolean variable A is true and variable B is false.
| Operator | Description | Example |
|---|---|---|
| && | Called the logical AND operator. The condition is true if and only if both operands are true. | (A && B) is false. |
| | | | Called the logical OR operator. If either of the two operands is true, the condition is true. | (A || B) is true. |
| ! | Called the logical NOT operator. It is used to reverse the logical state of the operand. If a condition is true, then the logical NOT operator will produce false. | !(A && B) is true. |
Example
The simple example program below demonstrates logical operators. Copy and paste the following Java program, save it as a Test.java file, then compile and run this program:
Example
The result of compiling and running the above example is as follows:
a && b = false a || b = true !(a && b) = true
Short-circuit Logical Operators
When using the logical AND operator, the result is true only when both operands are true, but when the first operand is false, the result is necessarily false, and the second operand is no longer evaluated.
Example
The output is:
使用短路逻辑运算符的结果为false a的结果为5
Analysis:This program uses the short-circuit logical operator (&&). First, the result of a<4 is determined to be false, so the result of b is necessarily false; therefore, the second operation a++<10 is not executed, so the value of a is 5.
Assignment Operators
The following are the assignment operators supported by the Java language:
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment operator: assigns the value of the right operand to the left operand. | C = A + B will assign the value of A + B to C. |
| + = | Addition and assignment operator: it adds the left and right operands and assigns the result to the left operand. | C += A is equivalent to C = C + A |
| - = | Subtraction and assignment operator: it subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |
| * = | Multiplication and assignment operator: it multiplies the left and right operands and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
| / = | Division and assignment operator: it divides the left operand by the right operand and assigns the result to the left operand. | C /= A, when C and A are of the same type, is equivalent to C = C / A |
| (%)= | Modulus and assignment operator: it takes the modulus of the left and right operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |
| << = | Left shift assignment operator | C <<= 2 is equivalent to C = C << 2 |
| >> = | Right shift assignment operator | C >>= 2 is equivalent to C = C >> 2 |
| &= | Bitwise AND assignment operator | C &= 2 is equivalent to C = C & 2 |
| ^ = | Bitwise XOR assignment operator | C ^= 2 is equivalent to C = C ^ 2 |
| | = | Bitwise OR assignment operator | C |= 2 is equivalent to C = C | 2 |
Example
The following simple example program demonstrates assignment operators. Copy and paste the following Java program, save it as a Test.java file, then compile and run this program:
Test.java file code:
The result of compiling and running the above example is as follows:
c = a + b = 30 c += a = 40 c -= a = 30 c *= a = 300 c /= a = 1 c %= a = 5 c <<= 2 = 20 c >>= 2 = 5 c >>= 2 = 1 c &= a = 0 c ^= a = 10 c |= a = 10
Conditional Operator (?:)
The conditional operator is also called the ternary operator. This operator has 3 operands and needs to evaluate a boolean expression. The main purpose of this operator is to decide which value should be assigned to a variable.
variable x = (expression) ? value if true : value if false
Example
Test.java file code:
The result of compiling and running the above example is as follows:
Value of b is : 30 Value of b is : 20
instanceof Operator
This operator is used to operate on object instances, checking whether the object is of a specific type (class type or interface type).
The usage format of the instanceof operator is as follows:
( Object reference variable ) instanceof (class/interface type)
If the object referred to by the variable on the left side of the operator is an object of the class or interface on the right side of the operator, the result is true.
Here is an example:
String name = "James"; boolean result = name instanceof String; // 由于 name 是 String 类型,所以返回真
If the object being compared is compatible with the right-side type, this operator still returns true.
Look at the following example:
The result of compiling and running the above example is as follows:
true
Java Operator Precedence
When multiple operators appear in an expression, which one comes first? This involves the issue of operator precedence. In a multi-operator expression, different operator precedence can lead to greatly different final results.
For example, (1+3) + (3+2)*2. If addition is calculated first with the highest precedence, the answer is 18; if multiplication is calculated first, the answer is 14.
Another example: x = 7 + 3 * 2; here x gets 13, not 20, because the multiplication operator has higher precedence than the addition operator, so 3 * 2 is calculated first to get 6, and then 7 is added.
In the table below, operators with the highest precedence are at the top, and those with the lowest precedence are at the bottom.
| Category | Operator | Associativity |
|---|---|---|
| Postfix | () [] . (dot operator) | Left to right |
| Unary | expr++ expr-- | Left to right |
| Unary | ++expr --expr + - ~ ! | Right to left |
| Multiplicative | * /% | Left to right |
| Additive | + - | Left to right |
| Shift | >> >>> << | Left to right |
| Relational | > >= < <= | Left to right |
| Equality | == != | Left to right |
| Bitwise AND | & | Left to right |
| Bitwise XOR | ^ | Left to right |
| Bitwise OR | | | Left to right |
| Logical AND | && | Left to right |
| Logical OR | | | | Left to right |
| Conditional | ?: | Right to left |
| Assignment | = + = - = * = / =%= >> = << =&= ^ = | = | Right to left |
| Comma | , | Left to right |