English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
One of the most basic uses of a computer is to perform mathematical operations, and 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
Relational operators
Bitwise operators
Logical operators
Assignment operator
Other operators
Arithmetic operators are used in mathematical expressions, and their functions are the same as in mathematics. The following table lists all arithmetic operators.
The examples in the table assume that the value of the integer variable A is10, the value of variable B is20:
Operators | Description | Example |
---|---|---|
+ | Addition - The values on both sides of the addition operator | A + B equals 30 |
- | Subtraction - The left operand minus the right operand | A - B equals -10 |
* | Multiplication - The values on both sides of the multiplication operator | A * B equals200 |
/ | Division - The left operand divided by the right operand | B / A equals2 |
% | Modulus - The remainder when the left operand is divided by the right operand | B % A equals 0 |
++ | Increment: The value of the operand is increased1 | B++ or ++B equals 21(Details see below) |
-- | Decrement: The value of the operand is decreased1 | B-- or --B equals 19(Details see below) |
The following simple example program demonstrates arithmetic operators. Copy and paste the following Java program into a file named ArithmeticOperator.java, then compile and run this program:
class ArithmeticOperator { public static void main(String[] args) { double number1 = 12.5, number2 = 3.5, result; //Using the addition operator result = number1 + number2; System.out.println("number1 + number2 = \ + result); //Using the subtraction operator result = number1 - number2; System.out.println("number1 - number2 = \ + result); //Using the multiplication operator result = number1 * number2; System.out.println("number1 * number2 = \ + result); //Using the division operator result = number1 / number2; System.out.println("number1 / number2 = \ + result); // Using the modulus operator result = number1 % number2; System.out.println("number1 % number2 = \ + result); } }
The compiled and run results of the above examples are as follows:
number1 + number2 = 16.0 number1 - number2 = 9.0 number1 * number2 = 43.75 number1 / number2 = 3.5714285714285716 number1 % number2 = 2.0
1, increment (++) decrement (--) operatoris a special arithmetic operator that requires two operands for operation among arithmetic operators, while the increment and decrement operators are single operands.
public class selfAddMinus{ public static void main(String[] args){ int a = 3;//Define a variable; int b = ++a;//Increment operation int c = 3; int d = --c;//Decrement operation System.out.println("The value after the increment operation is equal to"+b); System.out.println("The value after the decrement operation is equal to"+); } }
The output result is:
The value after the increment operation is equal to4 The value after the decrement operation is equal to2
Analysis:
int b = ++a; The operation process is split into: a=a+1=4; b=a;4, the final result is b=4, a=4
int d = --c; The operation process is split into: c=c-1=2; d=c;2, the final result is d=2, c=2
2, prefix increment and decrement (++a,--): First perform increment or decrement operations, then perform expression operations.
3, postfix increment and decrement (a++, a--) First perform expression operations, then increment or decrement operations Example:
public class selfAddMinus{ public static void main(String[] args){ int a = 5;//Define a variable; int b = 5; int x = 2*++a; int y = 2*b++; System.out.println("After the prefix increment operator operation, a="+a+",x="+); System.out.println("After the postfix increment operator operation, b="+b+",y="+); } }
The output result is:
, after the prefix increment operator operation, a=6, x=12, after the postfix increment operator operation, b=6, y=10
The following table lists the relational operators supported by Java
, the value of the integer variable A in the table is10, the value of variable B is20:
Operator | Description | Example |
---|---|---|
== | Check if the values of the two operands are equal, if equal, the condition is true. | (A == B) is false. |
!= | Check if the values of the two operands are equal, if not equal, the condition is true. | (A != B) is true. |
> | Check if 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. |
< | Check if 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. |
>= | Check if 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. |
<= | Check if 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. |
The following simple example program demonstrates relational operators. Copy and paste the following Java program and save it as Test.java file, then compile and run this program:
Test.java file code:
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 compiled and run results of the above examples 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 that are applied to integer types (int), long types (long), short types (short), character types (char), and byte types (byte) and other types.
Bitwise operators operate on all bits and perform bitwise operations. Assuming 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 bitwise operators, assuming the integer variable A has the value 6The value of variable B is 13:
Operators | Description | Example |
---|---|---|
& | If the corresponding bits are1Then the result is1Otherwise, it is 0 | (A&B), becomes12That is 0000 1100 |
| | If the corresponding bits are both 0, the result is 0, otherwise 1 | (A | B) becomes61That is 0011 1101 |
^ | If the corresponding bit values are the same, the result is 0, otherwise1 | (A ^ B) becomes49That is 0011 0001 |
~ | The bitwise NOT operator flips each bit of the operand, that is, 0 becomes1,1becomes 0. | (~A) becomes-61That is1100 0011 |
<< | Bitwise left shift operator. The left operand is shifted to the left by the number of bits specified by the right operand. | A << 2Get240, that is 1111 0000 |
>> | Bitwise right shift operator. The left operand is shifted to the right by the number of bits specified by the right operand. | A >> 2Get15That is 1111 |
>>> | Bitwise right shift with zero fill operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand, and the empty positions are filled with zeros. | A>>>2Get15That is, 0000 1111 |
The following simple example program demonstrates bitwise operators. Copy and paste the following Java program and save it as Test.java file, then compile and run this 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; /* 15 = 1111 */ System.out.println("a >> 2 = \ + c ); c = a >>> 2; /* 15 = 0000 1111 */ System.out.println("a >>> 2 = \ + c ); } }
The compiled and run results of the above examples 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 the boolean variable A is true and variable B is false
Operators | Description | Example |
---|---|---|
&& | It is called the logical AND operator. The condition is true only when both operands are true. | (A && B) is false. |
|| | It is called the logical OR operator. If any of the two operands is true, the condition is true. | (A || | B) is true. |
! | It is called the logical NOT operator. It is used to reverse 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 Test.java file, then compile and run this 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 compiled and run results of the above examples are as follows:
a && b = false a || b = true !(a && b) = true
When using logical operators, the result is true only when both operands are true, but when the first operand is false, the result is definitely false, and the second operand will no longer be judged at this time.
public class Logic{}} public static void main(String[] args){ int a = 5;//Define a variable; boolean b = (a<4)&&(a++<10); System.out.println("The result of using the short-circuit logical operator is"+b); System.out.println("The result of a is"+a); } }
The output result is:
The result of using the short-circuit logical operator is false The result of a is5
Analysis:This program uses the short-circuit logical operator (&&), first判断 a<4 The result is false, then the result of b must be false, so the second operation a is no longer executed++<10 judgment, so the value of a is 5.
The following are the assignment operators supported by the Java language:
Operators | Description | Example |
---|---|---|
= | Simple assignment operator, which assigns the value of the right operand to the left operand | C = A + B will assign A + The value obtained by B is assigned to C |
+ = | Addition and assignment operator, which assigns the sum of the left operand and the right operand to the left operand | C + = A is equivalent to C = C + A |
- = | Subtraction and assignment operator, which assigns the difference of the left operand and the right operand to the left operand | C - = A is equivalent to C = C - A |
* = | Multiplication and assignment operator, which assigns the product of the left operand and the right operand to the left operand | C * = A is equivalent to C = C * A |
/ = | Division and assignment operator, which assigns the division of the left operand and the right operand to the left operand | C / = A, equivalent to C = C when C and A are of the same type / A |
(%) = | Modulo and assignment operator, which assigns the modulo of the left operand and the right operand to the left operand | C %= A is equivalent to C = C % A |
<< = | Left shift assignment operator | C << = 2is equivalent to C = C << 2 |
>> = | Right shift assignment operator | C >> = 2is equivalent to C = C >> 2 |
& = | Bitwise AND assignment operator | C & = 2is equivalent to C = C &2 |
^ = | Bitwise XOR assignment operator | C ^ = 2is equivalent to C = C ^ 2 |
| = | Bitwise OR assignment operator | C | = 2is 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 AssignmentOperator.java file, then compile and run this program:
class AssignmentOperator { public static void main(String[] args) { int number1, number2; //Assign5to number1 number1 = 5; System.out.println(number1); //Assign the variable number2is assigned to number1 number2 = number1; System.out.println(number2); } }
The compiled and run results of the above examples are as follows:
5 5
The conditional operator is also known as the ternary operator. This operator has3An operand, and it needs to determine the value of the 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; // If a is equal to 1 If it is true, set b to 20, otherwise it is 30 b = (a == 1)? 20 : 30; System.out.println("Value of b is:"); + b); // If a is equal to 10 If it is true, set b to 20, otherwise it is 30 b = (a == 10)? 20 : 30; System.out.println("Value of b is:"); + b); } }
The compiled and run results of the above examples 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 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 a class or interface (class/If an object of an interface is on the right side, the result is true.
Here is an example:
String name = "James"; boolean result = name instanceof String; // Since name is of type String, it returns true
If the object being compared is compatible with the type on the right, the operator still returns true.
See 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 compiled and run results of the above examples are as follows:
true
When multiple operators appear in an expression, which one comes first? This involves the issue of operator precedence. Different operator precedence in a multi-operator expression can lead to greatly different results.
For example,(1+3) + (3+2)*2, if this expression is calculated with the plus sign as the most prioritized, the answer would be 18, if the multiplication operator is the most prioritized, the answer would be 14.
Again, x = 7 + 3 * 2; here x gets13, not20, because the multiplication operator has a higher priority than the addition operator, so it is calculated first3 * 2Get6, and then add7.
The operators with the highest priority in the table are at the top, and the lowest priority are at the bottom.
Category | Operators | 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 |
Shifting | >> >>> << | 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 |