[Java02] Operator
Arithmetic Operators
Java uses four types of operations: +,-, *, And/, indicating addition, subtraction, multiplication, and division. % indicates the remainder operation. If the two numbers involved in the operation are integers, the integer division is used. Otherwise, the floating point division is used. 15/7 = 2.0% 2 = 7.5/=.
Simplified Binary Arithmetic Operator number:
+ =
X + = 4, equivalent to x = x + 4-=
X-= 4 is equivalent to x = X-4 * =
X * = 2 is equivalent to x = x * 2/=
X/= 2 is equivalent to x = x/2.
Auto-increment and auto-increment Operators
Prefix auto-increment: ++ n
Suffix auto-increment: n ++
int a=6;int b=6;int m=++a //m=7,a=7int n=b++ // n=b,b=7
During prefix operations, n is first added with 1 before the operation. During suffix operations, n is first computed with 1 after the operation.
Relational operators
| Operator |
Function |
| = |
Checks whether values of equal signs are equal. |
| ! = |
Checks whether the values on both sides of the equal sign are not equal. |
| > |
Greater |
| < |
Less |
| > = |
Greater than or equal |
| <= |
Less than or equal |
Logical operators
& Indicates logic and. If A & B is true only when A and B are true, A | B. If either A or B is true, A | B is true. & | calculate the value based on the short-circuit mode. If the first operand can determine the result of the expression, the second operand is no longer calculated.
x!=0 && 1/x>x+y
Bitwise operators
Four bitwise operators:
&
Take 29 as an example. The binary value of 29 is (11101), the binary value of 16 is (10000), and the result of 29 & 16 is 16 (1 0000)
System.out.println(29&16); //16
| Or ^ exclusive or ~ Non <left shift
Move left is similar to move the decimal point. move the decimal point to the left by 10 times, and move the decimal point to the left by 2 times in the computer. Shift n places to the left to enlarge the n power of 2.> shift right
The right shift is the opposite of the Left shift.
int a=16;System.out.println(a>>2); //a=4System.out.println(a<<2); //a=64