catalogue
Continued above
6. Java operators
Java operators are mainly divided into arithmetic operators, relational operators, bit operators, logical operators, assignment operators and other operators.
6.1 arithmetic operators
operator | describe | Example a = 3 , b = 2 | Value of c |
+ | Add operator add on both sides | c = a + b | 5 |
- | Subtract the right from the left of the minus operator | c = a - b | 1 |
* | Multiply operator multiplies both sides | c = a * b | 6 |
/ | Divide - Operator left divided by right | c = a / b | 1 |
% | The remainder operator divides the left side of the operator by the right side and returns the remainder | c = a % b | 1 |
++ | Increment - the value of the operand plus 1 | c = a++ c = ++a | c = 3 , a = 4 c = 5 , a = 5 |
-- | Self subtraction - the value of the operand minus 1 | c = b-- c= --b | c = 2 , b = 1 c = 0 , b = 0 |
Other arithmetic operators need two operands to operate, but the self increasing and self decreasing operator only needs one operand.
c = a + + is equivalent to c = a; a = a+1 ;
c = ++a is equivalent to a = a + 1; c = a ;
c = b -- equivalent to c = b; b = b-1 ;
c = --b is equivalent to B = B-1; c = b;
Prefix self increment and self decrement (+ + A, - a): first perform self increment or self decrement operation, and then perform expression operation
Suffix self increment and self decrement (a + +, a --): first perform expression operation, and then perform self increment or self decrement operation
Code demonstration:
public class CSDNTest { public static void main(String[] args) { int a = 3; int b = 2; int c ; c = a+b; System.out.println("c = a + b ,c The value of is: "+c); c = a-b; System.out.println("c = a - b ,c The value of is: "+c); c = a*b; System.out.println("c = a * b ,c The value of is: "+c); c = a/b; System.out.println("c = a / b ,c The value of is: "+c); c = a%b; System.out.println("c = a % b ,c The value of is: "+c); c = a++; System.out.println("c = a++ ,c The value of is: "+ c + ", a The value of is:" + a); c = ++a; System.out.println("c = --a ,c The value of is: "+ c + ", a The value of is:" + a); c = b--; System.out.println("c = b-- ,c The value of is: "+ c + ", b The value of is:" + b); c = --b; System.out.println("c = --b ,c The value of is: "+ c + ", b The value of is:" + b); } }
Operation results:
6.2 relational operators
The result of relational operation is boolean value.
operator | describe | Example a = 2 , b = 3 | Operation result |
== | Equal: whether the left and right sides of the comparison operator are equal. The operation result is a boolean value | a == b | false |
!= | Not equal: whether the left and right sides of the comparison operator are not equal. The operation result is a boolean value | a != b | true |
> | Greater than: check whether the left side of the operator is greater than the right side, and the operation result is a boolean value | a > b | false |
< | Less than: check whether the left side of the operator is less than the right side, and the operation result is a boolean value | a < b | true |
>= | Not less than: check whether the left side of the operator is not greater than the right side, and the operation result is a boolean value | a >= b | false |
<= | Not greater than: check whether the left side of the operator is not less than the right side, and the operation result is a boolean value | a <= b | true |
Code demonstration:
public class CSDNTest { public static void main(String[] args) { int a = 2; int b = 3; boolean c ; c = a==b; System.out.println("c = a==b ,c The value of is: "+c); c = a!=b; System.out.println("c = a!=b ,c The value of is: "+c); c = a>b; System.out.println("c = a>b ,c The value of is: "+c); c = a<b; System.out.println("c = a<b ,c The value of is: "+c); c = a>=b; System.out.println("c = a>=b ,c The value of is: "+c); c = a<=b; System.out.println("c = a<=b ,c The value of is: "+c); } }
Operation results:
6.3 bit operators
Bitwise operators are applied to integer types (int), long, short, char, and byte. Bit operators act on all bits and operate bitwise.
operator | describe | Example a = 20 =1 0100B b = 13 = 1101B | Value of c |
& | And: bitwise and the operands on the left and right sides of the operator. 1 & 1 are 1 and the rest are 0 | c = a & b | 4, i.e. 100B |
| | Or: bitwise or the operands on the left and right sides of the operator, 0 | 0 is 0, and the rest are 1 | c = a & b | 29, i.e. 11101B |
^ | XOR: bitwise XOR the operands on the left and right sides of the operator. The difference is 1 and the same is 0 | c = a & b | 25, i.e. 11001B |
~ | Negate: negate the operand bit by bit, ~ 1 is 0, ~ 0 is 1 | c = ~a | -21, i.e. 1111 1111 1111 1111 1111 1110 1011b |
<< | Shift left n bits by bit: shift the operand to the left n bits by bit, which is equivalent to multiplying the operand by 2^n | c = a << 2 | 80, i.e. 1010000B |
>> | Shift the operand to the right by N bits, which is equivalent to dividing the operand by 2^n | c = a >> 2 | 5, i.e. 101B |
>>> | Shift n bits to the right by bit, and fill 0 in the empty bit | 5, i.e. 101B |
Operation results:
Signed numbers are stored in the form of complement in the computer. int data accounts for 4 bytes, that is, 32 bits.
6.4 logical operators
operator | describe | Example a = true , b = false | Operation result |
&& | Logical and: check whether the operands on the left and right sides of the operator are true. When both operands are true, the operation result is true, otherwise the operation result is false | a && b | false |
|| | Logical or: check whether the operands on the left and right sides of the operator are true. When both operands are false, the operation result is false; otherwise, the operation result is true | a || b | true |
! | Non: inverts the state of the operand,! True equals false,! False equals true | !a !b | false true |
Code demonstration:
public class CSDNTest { public static void main(String[] args) { boolean a = true; boolean b = false; boolean c ; c = a&&b; System.out.println("c = a&&b ,c The value of is: "+c); c = a||b; System.out.println("c = a||b ,c The value of is: "+c); c = !a; System.out.println("c = !a ,c The value of is: "+c); c = !b; System.out.println("c = !b ,c The value of is: "+c); } }
Operation results:
When & & the operand on the left is false, the operation result must be false, so whether the operand on the right is true or not is ignored
6.5 assignment operators
Operator | describe | Example | Value of c |
---|---|---|---|
= | Simple assignment operator: assign the value of the right operand to the left operand | b = a+5 | 8 |
+ = | Addition and assignment operator: assigns the result of the left operand plus the right operand to the left operand | c + = a is equivalent to c = c + a | 8 |
- = | Subtraction and assignment operator: assigns the result of subtracting the left operand from the right operand to the left operand | c - = a is equivalent to c = c - a | 5 |
* = | Multiplication and assignment operator: assigns the result of multiplying the left operand by the right operand to the left operand | c * = a is equivalent to c = c * a | 15 |
/ = | Division and assignment operator: assigns the quotient of the left operand divided by the right operand to the left operand | c / = a is equivalent to c = c / a | 5 |
%= | Modulo and assignment operators: assign the remainder of the left operand divided by the right operand to the left operand | C% = a is equivalent to c = c + a | 2 |
<< = | Left shift assignment operator: assigns the result of shifting the left operand by n bits to the left operand | C < < = a is equivalent to C = C < < a | 16 |
>> = | Right shift assignment operator: assigns the result of shifting the left operand right by n bits to the left operand | C > > = a is equivalent to C = C > > a | 2 |
&= | Bitwise and assignment operator: assigns the result of bitwise and after the left and right operands to the left operand | C & = a is equivalent to C = C & A | 2 |
^ = | Bitwise exclusive or assignment operator: assigns the result of bitwise exclusive or of the left and right operands to the left operand | c ^ = a is equivalent to c = c ^ a | 1 |
| = | Bitwise or assignment operator: assigns a bitwise or subsequent result of the left and right operands to the left operand | c | = a is equivalent to c = c | a | 3 |
Code demonstration:
public class CSDNTest { public static void main(String[] args) { int a = 3; int b; int c = 5; System.out.println("a The value of is: "+a); System.out.println("c The value of is: "+c); b = a+c; System.out.println("b = a+c ,b The value of is: "+b); c +=a; System.out.println("c +=a; ,c The value of is: "+c); c -=a; System.out.println("c -=a; ,c The value of is: "+c); c *=a; System.out.println("c *=a; ,c The value of is: "+c); c /=a; System.out.println("c /=a; ,c The value of is: "+c); c %=a; System.out.println("c %=a; ,c The value of is: "+c); c <<=a; System.out.println("c <<=a ,c The value of is: "+c); c >>=a; System.out.println("c >>=a ,c The value of is: "+c); c &=a; System.out.println("c &=a ,c The value of is: "+c); c ^=a; System.out.println("c ^=a ,c The value of is: "+c); c |=a; System.out.println("c |=a ,c The value of is: "+c); } }
Operation results:
Remember: move the left translation of the equal sign to the right, and the left operand is equal to the expression after moving.
6.6 conditional operator (?:)
The conditional operator, also known as the ternary operator, has three operands. You need to judge whether the value of the first operand is true. If it is true, the operation result is the value of the second operand; If false, the result is the value of the third operand.
Code demonstration:
public class CSDNTest { public static void main(String[] args) { int a = 3; int b = 5; int c; System.out.println("a The value of is: "+a); System.out.println("b The value of is: "+5); c = a>b?a:b; System.out.println("a and b In, the maximum value is: "+c); c = a<b?a:b; System.out.println("a and b In, the minimum value is: "+c); } }
Operation results:
6.7. instanceof operator
instanceof operator is a binary operator, which is used to check whether an object instance is an instance of a specific type or its subclass (class type or interface type), and the operation result is a boolean value.
boolean result = object instanceof class;
6.8 operator priority
When multiple operators appear at the same time, the priority of operators is involved.
Order priority from high to low:
category | Operator | Associativity |
---|---|---|
suffix | () [] . (point operator) | Left to right |
one yuan | expr++ expr -- (suffix self increasing and self decreasing) | Left to right |
one yuan | ++expr --expr + - ~ ! (prefix self increasing and self decreasing, sign negative, non) | Right to left |
Multiplicity | */% (multiplication, division and remainder) | Left to right |
Additive | +- (plus and minus) | Left to right |
Shift | < < > > > > > (shift left, shift right and shift right by bit, and fill in 0) | Left to right |
Relationship | >> = < < = (greater than, greater than or equal to, less than or equal to) | Left to right |
Equal | == != (equal to, not equal to) | 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 |
Unfinished to be continued...