The best Java operator Tutorial In 2024, In this tutorial you can learn Arithmetic operators,Relational Operators,Bitwise Operators,Logical Operators,Assignment Operators,Conditional operator (? :),instanceOf operator,Java operator precedence,
One of the most basic use of a computer is to perform mathematical operations, as a computer language, Java provides a rich set of operators to manipulate variables. We can put the operator into the following groups:
Arithmetic operators used in mathematical expressions, their role and the role of mathematics in the same. The following table lists all the arithmetic operators.
Table example assumes integer variable A value of 10, the value of the variable B 20:
Operators | description | example |
---|---|---|
+ | The added value of the operator on both sides - Addition | A + B is equal to 30 |
- | Subtraction - minus the left operand to the right operand | A - B is -10 |
* | Multiplication - multiplied both sides of the operator | A * B is equal to 200 |
/ | Division - the left operand divided by the right operand | B / A is equal to 2 |
% | Modulo - in addition to the remainder of the left operand and right operand | B% A equals 0 |
+ + | Increment - increase the value of the operand 1 | B + + is equal to 21 |
- | Decrement - decrease the value of the operand 1 | B - - equal to 19 |
The following simple example program demonstrates the arithmetic 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 = 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 above examples compiled results 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
The following table shows the supported Java relational operators
Is 10, the variable B table instance integer variable A value of 20:
Operators | description | example |
---|---|---|
== | Check if the value of the two operands are equal, equal, if the condition is true. | (A == B) is false (not true). |
! = | Check if the value of the two operands are equal, if the values are not equal then the condition is true. | (A! = B) is true. |
> | Check the value of the left operand is greater than the value of the right operand, if it is then the condition is true. | (A> B) is not true. |
< | Check the value of the left operand is less than the value of the right operand, if it is then the condition is true. | (A <B) is true. |
> = | Check the value of the left operand is greater than or equal to the value of the right operand, if it is then the condition is true. | (A> = B) is false. |
<= | Check the value of the left operand is less than or equal to the value of the right operand, if it is then 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:
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 examples compiled results are as follows:
a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = false
Java defines the bit operator, applied to integer type (int), long integer (long), short integer (short), character (char), and byte (byte) and other types.
Bitwise operators in all positions, and bitwise operations. Suppose a = 60, b = 13; their binary format 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 Bitwise operators, assuming integer variable A variable B is 60 and the value of 13:
Operators | description | example |
---|---|---|
& | Bitwise and operator, if and only if a bit in both operands are non-zero result when the bit is 1. | (A & B), to give 12, namely 0000 1100 |
| | Bitwise or operator, a bit as long as the two operands have a non-zero result when this bit is set to 1. | (A | B) to give 61, namely 00,111,101 |
^ | Bitwise XOR operator, a bit of the two operands are not the same result when the bit is set to 1. | (A ^ B) to give 49, namely 00,110,001 |
~ | Bitwise complement operator flipping each bit operands. | (~A) -61 Obtained, namely 11,000,011 |
<< | Bitwise left shift operator. The left operand by the right operand bit left shift specified number of digits. | A << 2 of 240, ie 11.11 million |
>> | Bitwise right shift operator. Left operand specifies the number of bits by bit to the right of the right operand. | A >> 2 get 15 ie 1111 |
>>> | Bitwise right zeros operator. The value of the left operand Press the right operand the specified number of bits to the right, move the resulting vacancies are filled with zeros. | A >>> 2 get 15 ie 0000 1111 |
The following simple example program demonstrates the 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 above examples compiled results are as follows:
a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 15 a >>> 15
The following table lists the basic operations logical operators, Boolean variables A hypothesis is true, the variable B is false
Operators | description | example |
---|---|---|
&& | Called a logical AND operator. If and only if both operands are true, the condition will be true. | (A && B) is false. |
| | | Called logical or operator. If any one of any two operands is true, the condition is true. | (A | | B) is true. |
! | Called logical NOT operator. It inverts the logic state of the operand. If the condition is true, the logical NOT operator will be false. | ! (A && B) is true. |
The following simple example program demonstrates the 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 above examples compiled results are as follows:
a && b = false a || b = true !(a && b) = true
Here is the Java language to support the assignment operator:
Operators | description | example |
---|---|---|
= | Simple assignment operator, the value of the right operand to the left-hand operand | C = A + B + B will give A value assigned to C |
+ = | Addition and assignment operator, it is the left operand and the right operand by adding an assignment to the left operand | C + = A is equivalent to C = C + A |
- = | Save left operand and assignment operator, it is the left operand and the right operand assigned to subtract | C - = A is equivalent to the C = C - A |
* = | Multiplication and assignment operator, it is the left operand and the right operand is multiplied by the assignment to the left operand | C * = A is equivalent to C = C * A |
/ = | In addition and assignment operator, it is the left operand and the right operand division assigned to the left operand | C / = A is equivalent to the C = C / A |
(%) = | Modulo and assignment operator, it is the left operand and the right operand to the left operand after assignment to modulo | C% = A is equivalent to the C = C% A |
<< = | Left-shift assignment operator | C << = 2 is equivalent to the C = C << 2 |
>> = | Right shift assignment operator | C >> = 2 is equivalent to the C = C >> 2 |
& = | Bitwise AND assignment operator | C & = 2 is equivalent to the C = C & 2 |
^ = | Bitwise XOR assignment operator | C ^ = 2 is equivalent to the C = C ^ 2 |
| = | Bitwise OR assignment operator | C | = 2 is equivalent to the C = C | 2 |
A simple example demonstrates the programs face assignment operator. 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 = 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 above examples compiled results 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
The conditional operator is also known as the ternary operator. The operator has three operands, and the need to judge the value of a Boolean expression. This operator is primarily 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 above examples compiled results are as follows:
Value of b is : 30 Value of b is : 20
This operator is used to operate the object instance, check whether the object is a particular type (class or interface type).
instanceof operator Use the following format:
( Object reference variable ) instanceOf (class/interface type)
If the operator is on the left side variable refers to the object, the operator or the right side of the interface class (class / interface) of an object, then 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 of the type, the operator still returns true.
Look at the following examples:
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 above examples compiled results are as follows:
true
When more than one operator appears in an expression, after whoever who? This relates to the priority of operator problems. In a multi-operator expression, operator precedence may lead to different results finally come to vary widely.
For example, (1 + 3) + (3 + 2) * 2, press the plus sign if this expression to calculate the highest priority, the answer is 18, according to the multiplication highest priority, the answer is 14.
Again, x = 7 + 3 * 2; where x had 13 instead of 20, because multiplication operator has a higher precedence than the addition operator, first calculate 3 * 2 get 6 and 7 plus.
The table top has the highest priority of the operators in the table, the lowest priority in the bottom of the table.
category | Operators | Relevance |
---|---|---|
suffix | () []. (Dot operator) | Left to Right |
One yuan | + + -! ~ | Right to left |
Multiplicative | * /% | Left to Right |
Additive | + - | Left to Right |
Displacement | >> << >>> | 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 |