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
- Relation operators
- Bit operator
- Logical Operators
- Assignment operator
- Other operators
Arithmetic operators
Arithmetic operators are used in mathematical expressions, and their role is the same as in mathematics. The following table lists all the arithmetic operators.
The examples in the table assume that the value of the integer variable A is 10 and the value of variable B is 20:
Operators | description | example |
---|---|---|
+ | Addition - Adds values on both sides of the operator | A + B is equal to 30 |
- | Subtraction - left operand minus right operand | A – B is equal to -10 |
* | Multiplication - Values on both sides of the multiplication operator | A * B is equal to 200 |
/ | Division - left operand divided by right operand | B / A is equal to 2 |
% | Modulo - The remainder of dividing the left operand by the right operand | B%A is equal to 0 |
++ | Self-increment: The value of the operand increases by 1 | B++ or ++B equals 21 (see below for details) |
-- | Decrease: The value of the operand decreases by 1 | B-- or -B equals 19 (see below for details) |
Examples
The following simple example program demonstrates arithmetic operators. Copy and paste the following Java program and save it as Test.java file, then compile and run the program:
Examples
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 -- ) ) ;
// View the difference between d++ and ++d System . out . println ( " d++ = " + ( d ++ ) ) ; System . out . println ( " ++d = " + ( ++ d ) ) ; }
}
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 -- ) ) ;
// View the difference between d++ and ++d System . out . println ( " d++ = " + ( d ++ ) ) ; System . out . println ( " ++d = " + ( ++ d ) ) ; }
}
The above example compiled and run results are as follows:
a + b = 30+ b = 30 a - b = -10- b = - 10 a * b = 200* b = 200 b / a = 2/ a = 2 b % a = 0% a = 0 c % a = 5% a = 5 a++ = 10++ = 10 A-- = 11-- = 11 d++ = 25++ = 25 ++d = 27++ d = 27
Self-incrementing and decrementing operators
1. The auto-increment (++) decrement (--) operator is a special kind of arithmetic operator in which two operands are required to perform the operation, and the auto-increment and decrement operator is an operand. .
Examples
public class selfAddMinus {
public static void main ( String [ ] args ) {
int a = 3 ; // Define a variable; int b = ++ a ; // Incremental operation int c = 3 ; int d = -- c ; // self-subtracting System.out.println( " self-increase operation is equal to a value obtained by " + b ) ; System.out.println( " value after decrement is equal to " + d ) ; }
}
public static void main ( String [ ] args ) {
int a = 3 ; // Define a variable; int b = ++ a ; // Incremental operation int c = 3 ; int d = -- c ; // self-subtracting System.out.println( " self-increase operation is equal to a value obtained by " + b ) ; System.out.println( " value after decrement is equal to " + d ) ; }
}
The result of the operation is:
The self-increment value equals 44 The value after decrementing is equal to 2The value after decrementing is equal to 2
Analysis:
- Int b = ++a; The splitting operation is: a=a+1=4; b=a=4, and the final result is b=4,a=4
- Int d = --c; The splitting operation is: c=c-1=2; d=c=2, and the final result is d=2,c=2
2. Prefix auto-increment and subtraction (++a, -a): First perform auto-increment or auto-decrement, and then perform expression operation.
3. Suffix self-increment and subtraction (a++, a--): Perform expression calculation first, and then increase or decrease automatically.
Examples
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( " Increment operator prefix a= " + a + " , x= " + x ) ; System.out.println( " Increment operator suffix operation b= " + b + " , y= " + y ) ; }
}
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( " Increment operator prefix a= " + a + " , x= " + x ) ; System.out.println( " Increment operator suffix operation b= " + b + " , y= " + y ) ; }
}
The result of the operation is:
After auto-increment operator prefix operation a=6, x=12a = 6 , x = 12 After the suffix operation of auto-increment operator b=6, y=10After the suffix of the auto-increment operator, b = 6 , y = 10
Relation operators
The following table shows Java-supported relational operators
The value of the instance integer variable A in the table is 10, and the value of variable B is 20:
Operator | description | example |
---|---|---|
== | Checks if the two operands are equal in value and if they are equal the condition is true. | (A == B) is false (not true). |
!= | Checks if the values of the two operands are equal and the condition is true if the values are not equal. | (A != B) is true. |
> | Checks if the value of the left operand is greater than the value of the right operand, and if so then the condition is true. | (A> B) is not true. |
< | Checks if the value of the left operand is less than the value of the right operand, and if so then the condition is 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, and if so then 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, and if so the condition is true. | (A <= B) is true. |
Examples
The following simple example program demonstrates the relational operators. Copy and paste the following Java program and save it as Test.java file, then compile and run the 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 . the println ( " B> A = = " + ( B > = A ) ) ; System . out . println ( " b <= a = " + ( b <= a ) ) ; }
}
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 . the println ( " B> A = = " + ( B > = A ) ) ; System . out . println ( " b <= a = " + ( b <= a ) ) ; }
}
The above example compiled and run results are as follows:
a == b = false== b = false a != b = true!= b = true a > b = false> b = false a < b = true< b = true b >= a = true>= a = true b <= a = false<= a = false
Bit operator
Java defines bit operators for integer types (int), longs, shorts, chars, and bytes.
The bit operator acts on all bits and is bitwise. Assume that a = 60, b = 13; their binary format will be as follows:
A = 0011 1100= 0011 1100 B = 0000 1101= 0000 1101 ---------------------------------- A&b = 0000 1100& b = 0000 1100 A | B = 0011 1101| B = 0011 1101 A ^ B = 0011 0001^ B = 0011 0001 ~A= 1100 0011~ A = 1100 0011
The following table lists the basic operations of the bit operator, assuming that the value of the integer variable A is 60 and the value of the variable B is 13:
Operators | description | example |
---|---|---|
& | If the corresponding bit is 1, the result is 1, otherwise it is 0 | (A&B), get 12, ie 0000 1100 |
| | If the corresponding bit is 0, the result is 0, otherwise it is 1 | (A | B) gets 61, ie 0011 1101 |
^ | If the corresponding bit value is the same, the result is 0, otherwise it is 1 | (A ^ B) gets 49, ie 0011 0001 |
~ | The bitwise inversion operator flips each bit of the operand, ie 0 becomes 1 and 1 becomes 0. | (~A) gets -61, that is, 1100 0011 |
<< | Bitwise left shift operator. The left operand is shifted left by the bit specified by the right operand. | A << 2 gets 240, that is 1111 0000 |
>> | Bitwise right shift operator. The left operand is shifted right by the number of bits specified by the right operand. | A >> 2 gets 15 that is 1111 |
>>> | Bitwise right shift padding operator. The value of the left operand is shifted right by the number of bits specified by the right operand, and the resulting gap is filled with zeros. | A>>>2 gets 15 that is 0000 1111 |
Examples
The following simple example program demonstrates the bit operator. Copy and paste the following Java program and save it as Test.java file, then compile and run the program:
Test.java file code:
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 ) ; }
}
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 above example compiled and run results are as follows:
a & b = 12& b = 12 a | b = 61| b = 61 a ^ b = 49^ b = 49 ~a = -61~ a = - 61 a << 2 = 240<< 2 = 240 a >> 2 = 15>> 2 = 15 a >>> 2 = 15>>> 2 = 15
Logical Operators
The following table lists the basic operations of logical operators. Assume that Boolean variable A is true and variable B is false.
Operators | description | example |
---|---|---|
&& | Called logical AND operators. The condition is true if and only if both operands are true. | (A && B) is false. |
| | | | Called logical OR operator. If either of the two operands is true, the condition is true. | (A | | B) is true. |
! | It is called a logical non-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. |
Examples
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 the program:
Examples
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 ) ) ; } }
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 above example compiled and run results are as follows:
a && b = false&& b = false a || b = true|| b = true !(a && b) = true!( a && b ) = true
Short circuit logic operator
When using AND operators, the result is true when both operands are true, but when the first operation is false, the result must be false. One operation.
Examples
public class LuoJi {
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 a short-circuited logical operator is " + b ) ; System . out . println ( " The result of a is + " a ) ; }
}
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 a short-circuited logical operator is " + b ) ; System . out . println ( " The result of a is + " a ) ; }
}
The result of the operation is:
Using a short-circuited logical operator results in falseFalse The result of a is 5The result is 5
Analysis: This program uses the short-circuit logical operator (&&). First of all, if the result of a<4 is false, the result of b must be false. Therefore, the second operation a++<10 is no longer performed, so the value of a is not. It is 5.
Assignment operator
The following are the assignment operators supported by the Java language:
Operators | description | example |
---|---|---|
= | A simple assignment operator assigns the value of the right operand to the left operand | C = A + B assigns the value of A + B to C |
+ = | Add and assignment operator, which assigns the left and right operands to the left operand | C + = A is equivalent to C = C + A |
- = | Subtraction operator, which assigns the left and right operands to the left operand | C - = A is equivalent to C = C - A |
* = | Multipliers and assignment operators, which assign the left and right operands to the left operand | C * = A is equivalent to C = C * A |
/ = | Divide and assignment operators, which divide the left and right operands to the left operand | C / = A is equivalent to C = C / A |
(%)= | Modulo and assignment operator, which assigns left and right operands after modulo left and right operands | 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 operators | C&= 2 is equivalent to C = C&2 |
^ = | Bitwise XOR assignment operator | C ^ = 2 is equivalent to C = C ^ 2 |
| = | Bitwise or assignment operators | C | = 2 is equivalent to C = C | 2 |
Examples
The simple example program for the surface demonstrates the assignment operator. Copy and paste the following Java program and save it as Test.java file, then compile and run the program:
Test.java file code:
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; The System . OUT . The println ( " C + = A = " + C ) ; C - = A ; the System . OUT . The println ( " C - = A = " + C ) ; C * = A ; the System . OUT . The 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 . The println ( " C & = 2 = " + C ) ; C ^ = A ; the System . OUT . The println ( " C ^ = A = " + C ) ; C | = A ; the System . OUT . The println ( " C | = a = " + c ) ; }
}
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; The System . OUT . The println ( " C + = A = " + C ) ; C - = A ; the System . OUT . The println ( " C - = A = " + C ) ; C * = A ; the System . OUT . The 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 . The println ( " C & = 2 = " + C ) ; C ^ = A ; the System . OUT . The println ( " C ^ = A = " + C ) ; C | = A ; the System . OUT . The println ( " C | = a = " + c ) ; }
}
The above example compiled and run results are as follows:
c = a + b = 30= a + b = 30 c += a = 40+= a = 40 c -= a = 30-= a = 30 c *= a = 300*= a = 300 c /= a = 1/= a = 1 c %= a = 5%= a = 5 c <<= 2 = 20<<= 2 = 20 c >>= 2 = 5>>= 2 = 5 c >>= 2 = 1>>= 2 = 1 c &= a = 0&= a = 0 c ^= a = 10^= a = 10 c |= a = 10|= a = 10
Conditional operator (?:)
Conditional operators are also called ternary operators. This operator has 3 operands and needs to determine 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 = ( expression ) ? value if true : value if false
Examples
Test.java file code:
public class Test {
public static void main ( String [ ] args ) {
int a , b ; a = 10 ; // If a is equal to 1 then b is set 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, then b is set to 20, otherwise it is 30 . b = ( a == 10 ) ? 20 is : 30 ; System.out.println ( " the Value of B IS: " + b ) ; }
}
public static void main ( String [ ] args ) {
int a , b ; a = 10 ; // If a is equal to 1 then b is set 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, then b is set to 20, otherwise it is 30 . b = ( a == 10 ) ? 20 is : 30 ; System.out.println ( " the Value of B IS: " + b ) ; }
}
The above example compiled and run results are as follows:
Value of b is : 30Of b is : 30 Value of b is : 20Value of b is : 20
Instanceof operator
This operator is used to manipulate the object instance and check if the object is of a specific type (class type or interface type).
The instanceof operator uses the following format:
( Object reference variable ) instanceof (class/interface type) Object reference variable ) instanceof ( class / interface type )
The result is true if the object to which the left variable of the operator refers to an object of the right class or interface (class/interface) of the operator.
Below is an example:
String name = "James";Name = "James" ; Boolean result = name instanceof String; // Since name is a String type, it returns trueBoolean result = name instanceof String ; // Returns true since name is of type String
If the object being compared is compatible with the right type, the operator still returns true.
Take a 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 ) ; }
}
}
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 above example compiled and run results are as follows:
True
Java operator priority
When multiple operators appear in one expression, who is first? This relates to the issue of the operator's priority level. In a multi-operator expression, different operator precedence can result in very different results.
For example, (1+3)+(3+2)*2. If the expression is evaluated with the plus sign first, the answer is 18, and if the answer is the highest, the answer is 14.
Again, x = 7 + 3 * 2; where x gets 13 instead of 20, because the multiplication operator has a higher priority than the addition operator, so calculate 3 * 2 to get 6, and then add 7.
The operator with the highest priority in the following table is at the top of the table, and the lowest priority is at the bottom of the table.
category | Operators | Relevance |
---|---|---|
suffix | () [] . (dot operator) | Left to right |
One yuan | + + - ! ~ | Right to left |
Multiplicity | * /% | Left to right |
Additive | + - | Left to right |
Shift | >> >>> << | Left to right |
relationship | >> = << = | 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 | ? : | Right to left |
Assignment | = + = - = * = / =%= >> = << =&= ^ = | = | Right to left |
comma | , | Left to right |
Comments
Post a Comment