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 to manipulate variables. We can divide the operators into the following groups:
Arithmetic operators
Relational operators
Bit operators
- ## Logical Operators
- Assignment operator
- Other operators
Operator | describe | example |
---|---|---|
+ | Addition - the values on both sides of the addition operator | A + B equals 30 |
- | Subtraction - left operand minus right operand | A – B is equal to -10 |
* | Multiplication - the values on both sides of the multiplication operator | A * B equals 200 |
/ | Division - divide 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 is equal to 0 |
+ + | Auto-increment - the value of the operand is increased by 1 | B++ is equal to 21 |
-- | Decrement -- the value of the operand is reduced by 1 | B - -Equals 19 |
Example
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) ); } }
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 = 27
Relational operators
The following table shows the relational operators supported by Java
The example in the table has the value of integer variable A as 10 and the value of variable B as 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. |
Example
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 above example compilation and running results are as follows:
a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = false
Bit operator
Java defines bit operators, which are applied to integer types (int), long integer (long), short integer (short), character type (char), and byte type (byte).
Bitwise operators operate on all bits and operate bitwise. Suppose a = 60, 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 bit operators, assuming that the value of integer variable A is 60 and the value of variable B is 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) gets 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 gets 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 |
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; /* 15 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 15 = 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 >> 15 a >>> 15
Logical operatorThe following table lists the basic operations of logical operators, assuming that Boolean variable A is true and variable B is false
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. |
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 |
- = | The 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 |
/ = | Division and assignment operators, which divide the left operand and the right operand and assign the value to the left operand | C / = A is equivalent to C = C / A |
(%)= | Modulo and assignment operators, which modulo the left and right operands and assign the values 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 | ## ^ = |
C ^ = 2 is equivalent to C = C ^ 2 | ||
| = | Bitwise OR assignment operator | C | = 2 is equivalent to C = C | 2 |
Example
The simple example program above 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 &= 2 = " + 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 operator ( ?:)
The conditional operator is also called the ternary operator. 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
Instance
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 above example compilation and running results are as follows:
Value of b is : 30 Value of b is : 20
instanceOf operator
This operator is used to operate object instances. Checks whether the object is of a specific type (class type or interface type).
The format of the instanceof operator is as follows:
( 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, then the result is true.
The following is an example:
String name = 'James'; boolean result = name instanceOf String; // 由于name是String类型,所以返回真
If the compared objects are compatible with the right-hand type, this operator still returns true.
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
Java operator priority
When multiple operations When symbols appear in an expression, who comes first and who comes last? 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 with 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, Then add 7 more.
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 | + + -!~ | From right to left |
Multiplicative property | * /% | 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 |
Logic and | && | Left to right |
Logical OR | | | | Left to right |
condition | ? : | From right to left |
Assignment | = + = - = * = / =%= >> = << =&= ^ = | = | From right to left |
Comma | , | Left to right |