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 to manipulate variables. We can divide operators into the following groups:
Arithmetic operators are used in mathematical expressions and they work the same way they do in mathematics. The following table lists all arithmetic operators.
The example in the table assumes that the integer variable A has a value of 10 and the variable B has a value of 20:
Operator | describe | example |
---|---|---|
+ | Addition – the values on either side of the addition operator | A + B equals 30 |
- | Subtraction - left operand minus right operand | A – B equals -10 |
* | Multiplication - Multiplies the values on either side of the operator | A * B equals 200 |
/ | Division - divides the left operand by the right operand | B/A equals 2 |
% | Modulo - the remainder of the left operand divided by the right operand | B%A equals 0 |
++ | Increment - increases the value of the operand by 1 | B++ or ++B equals 21 |
-- | Decrement - decreases the value of the operand by 1 | B-- or --B equals 19 |
Although both are self-increasing (their operation results are equal to B+1), there are still differences between B++ and ++B. ++B is an lvalue and operates directly in place (can be understood as + directly on variable B 1), B++ is an rvalue. When operating in the compiler, a temporary variable will be constructed first, and then the temporary variable will be used to calculate +1 and then assigned to B.
Therefore, in the example code below, when printing d++, it is found that the result is not +1 (the variable d is printed at this time, and the temporary variable of d is operated), but the result displayed in the next printed statement is +1 again. The final result (the value of the temporary variable is assigned to variable d). The result of printing ++d is directly +1.
The following simple example program demonstrates arithmetic operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
public class Test { public static void main(String args[]) { int a = 10; int b = 20; int c = 25; int d = 25; System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("a-- = " + (a--) ); // 查看d++ 与++d 的不同System.out.println("d++ = " + (d++) ); System.out.println("d = " + d); System.out.println("++d = " + (++d) ); } }
The compilation and running results of the above example are 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 =26 ++d = 27
The following table shows the relational operators supported by Java
The example integer variable A in the table has a value of 10 and variable B has a value of 20:
operator | describe | example |
---|---|---|
== | Checks if the values of the two operands are equal, if so then the condition is true. | (A == B) is false (not true). |
!= | Checks if the values of two operands are equal, if the values are not equal then the condition is true. | (A != B) is true. |
> | Checks if the value of the left operand is greater than the value of the right operand, if so then the condition becomes true. | (A>B) is not true. |
< | Checks if the value of the left operand is less than the value of the right operand, if so then the condition becomes true. | (A < B) is true. |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand, if so then the condition becomes true. | (A>=B) is false. |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand, if so then the condition becomes true. | (A <= B) is true. |
The following simple example program demonstrates relational operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
public class Test { public static void main(String args[]) { int a = 10; int b = 20; System.out.println("a == b = " + (a == b) ); System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); } }
The compilation and running results of the above example are as follows:
a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = false
Java defines bitwise operators, which apply to integer types (int), long integers (long), short integers (short), character types (char), and byte types (byte).
Bitwise operators operate on all bits and operate bitwise. Assume a = 60, and b = 13; their binary format 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 bitwise operators, assuming that the integer variable A has a value of 60 and the variable B has a value of 13:
Operator | describe | example |
---|---|---|
& | Bitwise AND operator, the result is 1 if and only if a certain bit of both operands is non-0. | (A&B), get 12, which is 0000 1100 |
| | Bitwise OR operator, as long as a certain bit of the two operands has a non-0, the result will be 1. | (A | B) gets 61, which is 0011 1101 |
^ | Bitwise XOR operator, when a certain bit of the two operands is different, the result bit is 1. | (A^B) gives us 49, which is 0011 0001 |
~ | The bitwise complement operator flips each bit of the operand. | (~A) gets -61, which is 1100 0011 |
<< | Bitwise left shift operator. The left operand is shifted left by the number of bits specified by the right operand. | A << 2 gives us 240, which is 1111 0000 |
>> | Bitwise right shift operator. The left operand is shifted bitwise right by the number of bits specified by the right operand. | A >> 2 gets 15 which is 1111 |
>>> | Bitwise right shift zero-padding operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand, and the resulting vacancies are filled with zeros. | A>>>2 gets 15 which is 0000 1111 |
The following simple example program demonstrates bitwise operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
public class Test { public static void main(String args[]) { int a = 60; /* 60 = 0011 1100 */ int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ System.out.println("a & b = " + c ); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c ); c = a ^ b; /* 49 = 0011 0001 */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = 1100 0011 */ System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } }
The compilation and running results of the above example are as follows:
a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 2 = 15 a >>> 2 = 15
The following table lists the basic operations of logical operators, assuming that Boolean variable A is true and variable B is false
Operator | describe | example |
---|---|---|
&& | Called the logical AND operator. A condition is true if and only if both operands are true. | (A && B) is false. |
| | | Called the logical OR operator. The condition is true if either of the two operands is true. | (A | | B) is true. |
! | Called the logical NOT operator. Used to invert the logical state of the operand. If the condition is true, the logical NOT operator will get false. | ! (A && B) is true. |
The following simple example program demonstrates logical operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
public class Test { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b)); System.out.println("a || b = " + (a||b) ); System.out.println("!(a && b) = " + !(a && b)); } }
The compilation and running results of the above example are as follows:
a && b = false a || b = true !(a && b) = true
The following are the assignment operators supported by the Java language:
Operator | describe | example |
---|---|---|
= | Simple assignment operator, assigns the value of the right operand to the left operand | C = A + B will assign the value obtained by A + B to C |
+ = | Addition assignment operator, which adds the left operand and right operand and assigns the value to the left operand | C + = A is equivalent to C = C + A |
- = | Subtraction and assignment operator, which subtracts the left operand and the right operand and assigns the value to the left operand | C - = A is equivalent to C = C - A |
* = | The multiplication and assignment operator, which multiplies the left operand and the right operand and assigns the value to the left operand | C * = A is equivalent to C = C * A |
/ = | The division and assignment operator divides the left operand and the right operand and assigns the value to the left operand. | C / = A is equivalent to C = C / A |
(%)= | Modulo and assignment operator, which modulo the left and right operands and assigns the value 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 |
The following simple example program demonstrates the assignment operator. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
public class Test { public static void main(String args[]) { int a = 10; int b = 20; int c = 0; c = a + b; System.out.println("c = a + b = " + c ); c += a ; System.out.println("c += a = " + c ); c -= a ; System.out.println("c -= a = " + c ); c *= a ; System.out.println("c *= a = " + c ); a = 10; c = 15; c /= a ; System.out.println("c /= a = " + c ); a = 10; c = 15; c %= a ; System.out.println("c %= a = " + c ); c <<= 2 ; System.out.println("c <<= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + c ); c >>= 2 ; System.out.println("c >>= a = " + c ); c &= a ; System.out.println("c &= a= " + c ); c ^= a ; System.out.println("c ^= a= " + c ); c |= a ; System.out.println("c |= a= " + c ); } }
The compilation and running results of the above example are 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 operators are also known as ternary operators. This operator has three operands and needs to evaluate the value of a Boolean expression. The main purpose of this operator is to decide which value should be assigned to the variable.
variable x = (expression) ? value if true : value if false
public class Test { public static void main(String args[]){ int a , b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); } }
The compilation and running results of the above example are as follows:
Value of b is : 30 Value of b is : 20
This operator is used to operate on object instances and check whether the object is of a specific type (class type or interface type).
The instanceof operator is used in the following format:
( Object reference variable ) instanceof (class/interface type)
If the object pointed to by the variable on the left side of the operator is an object of the class or interface (class/interface) on the right side of the operator, the result is true.
Here's an example:
String name = 'James'; boolean result = name instanceof String; // 由于name是String类型,所以返回真
This operator still returns true if the compared objects are compatible with the right-hand type.
Look at the following example:
class Vehicle {} public class Car extends Vehicle { public static void main(String args[]){ Vehicle a = new Car(); boolean result = a instanceof Car; System.out.println( result); } }
The compilation and running results of the above example are as follows:
true
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 priorities will lead to very different final results.
For example, (1+3) + (3+2)*2, if this expression is calculated according to the plus sign as the highest priority, the answer is 18, and if the multiplication sign is the highest priority, the answer is 14.
Another example, x = 7 + 3 * 2; here x gets 13, not 20, because the multiplication operator has a higher priority than the addition operator, so 3 * 2 is calculated first to get 6, and then 7 is added.
The operators with the highest precedence in the following table are at the top of the table, and the operators with the lowest precedence are at the bottom of the table.
category | Operator | relevance |
---|---|---|
suffix | () [] . (dot operator) | left to right |
one yuan | + + -! ~ | right to left |
multiplicative | */% | left to right |
Additivity | + - | left to right |
shift | >> >>> << | left to right |
relation | >> = << = | left to right |
equal | == != | 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 |
condition | ? : | right to left |
Assignment | = + = - = * = / =%= >> = << =&= ^ = | = | right to left |
comma | , | left to right |