operator
The Java language supports the following operators:
1. Arithmetic operators: +, -, *, /,%, + +--
2. Assignment operator:=
3. Relational operators: >, <, > =, < =, = == instanceof
4. Logical operators: & &, ||!
5. Bitwise operators: &, |, ^, ~, > >, <, > > (understand)
6. Conditional operator:?:
7. Assignment operators: + =, - =, * =/=
Arithmetic operator
Operator | describe | example |
---|---|---|
+ | Values on both sides of the addition operator | A + B equals 25 |
- | Subtraction - left operand minus right operand | A – B equals - 5 |
* | Values on both sides of the multiply multiply operator | A * B equals 150 |
/ | Division - left operand divided by right operand | B / A equals 0.6 |
% | Modulo - the remainder of the left operand divided by the right operand | B%A equals 0 |
++ | Auto increment - the value of the operand is incremented by 1 | B + + or + + B equals 11 |
-- | Self decrement - the value of the operand is decremented by 1 | B -- or -- B equals 10 |
Arithmetic operators: +, -, *, / operator application examples:
public class Demo05 { public static void main(String[] args) { int a = 10; int b = 15; System.out.println(a+b);//Output result: 25 System.out.println(a-b);//Output result: - 5 System.out.println(a*b);//Output result: 150 System.out.println(a/b);//Output result: 0. The result is wrong. It should be decimal. Because the type is int, decimal cannot be calculated, so you need to pay attention to type conversion in the operation System.out.println((double) a/b);//Output result: 0.6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 System.out.println(b%a);//Output result: 0 } }
Arithmetic operator: + +, - Operator application example:
public class Dmeo06 { public static void main(String[] args) { int a = 10; int b = a++;//After executing this line of code, assign a value to b first, and then increase it automatically //b = a + 1 int c = ++a;//After this line of code is executed, it is self incremented first, and then assigned a value to c System.out.println(a);//Output result: 12 System.out.println(b);//Output result: 10 because the output is still the original a value, the a + + self increment is calculated below this line. System.out.println(c);//Output result: 12 System.out.println(c--);//Output result: 12 because the output is still the original a value, the a + + self increment is calculated below this line. System.out.println(--c);//Output result: 10 } }
Relational operator
operator | describe | example |
---|---|---|
== | Checks if the values of the two operands are equal, and if they are equal, the condition is true. | (A == B) is true. |
!= | Checks if the values of the two operands are equal, and if the values are not equal, the condition is true. | (a! = b) is false. |
> | Check whether the value of the left operand is greater than the value of the right operand. If so, the condition is true. | (a > b) not false. |
< | Check whether the value of the left operand is less than the value of the right operand. If so, the condition is true. | (a < b) is false. |
>= | Check whether 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 true. |
<= | Check whether 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. |
Relational operators: >, <, > =, < =, = == Application example:
public class Demo07 { public static void main(String[] args) { int a = 120; int b = 120; //>,< , >= , <= , == , !=instanceof System.out.println(a==b);//Output result: true System.out.println(a!=b);//Output result: false System.out.println(a>b);//Output result: false System.out.println(a<b);//Output result: false System.out.println(a>=b);//Output result: true System.out.println(a<=b);//Output result: true } }
Logical operator
Operator | describe | |
---|---|---|
&& | They are called logical and operators. The condition is true if and only if both operands are true. | |
| | | It is called a logical or operator. If either of the two operands is true, the condition is true. | |
! | It is called a logical non operator. Used to reverse the logical state of an operand. If the condition is true, the logical non operator will get false. |
Logical operators: & &, |, |! Application example:
public class Demo07 { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a && b:"+(a && b));//Output result: A & & B: false System.out.println("a || b:"+(a || b));//Output result: a | B: true System.out.println("!(a && b):"+!(a && b));//Output result:! (a && b):true } }
Sort out the logic in the & & operator. The definition is that if and only if both operands are true, the condition is true. In the operation, if the first value judged is false, will the second value continue to be judged? Let me do an experiment to verify:
public class Demo07 { public static void main(String[] args) { int a = 5; boolean b = a<1 && ++a<1; System.out.println(b);//Output result: false System.out.println(a);//Output result: 5 } }
It can be seen from the above code that a < 1 is obviously wrong and does not increase automatically when outputting a, so the code behind the first value will not be executed when judging that the first value is false during & & operation. This should be noted.
Bitwise Operators
Operator | describe | |
---|---|---|
& | Bitwise and operator, if and only if one bit of the two operands is not 0, the bit of the result is 1. | |
| | Bitwise OR operator, as long as one of the two operands has a non-0, the bit of the result is 1. | |
^ | Bitwise XOR operator, when one bit of two operands is different, the bit of the result is 1. | |
〜 | The bitwise complement operator flips each bit of the operand. | |
<< | Bitwise left shift operator. The left operand shifts left by bits the number of bits specified by the right operand. | |
>> | Bitwise shift right operator. The left operand shifts the number of bits specified by the right operand bit by bit. | |
>>> | Bitwise shift right zeroing 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 bits are filled with zeros. |
Bitwise operators: &, |, ^, ~, > >, <, > > >
public class Demo07 { 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; /* 0000 1100 1100 in, move two bits to the high (left), 240 = 1111 0000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } }
<< | >> | >>> |
When moving to the left, no matter positive or negative, the low order is supplemented by 0 Positive number: r = 20 < < 2 Binary complement of 20: 0001 0100 After moving two digits to the left: 0101 0000 Result: r = 80 Negative number: r = - 20 < < 2 Binary original code of - 20: 1001 0100 Binary inverse of - 20 : 1110 1011 Binary complement of - 20: 1110 1100 Complement after two left shifts: 1011 0000 Inverse code: 1010 1111 Original code: 1101 0000 Result: r = -80 | Positive number: r = 20 > > 2 Binary complement of 20: 0001 0100 After moving two digits to the right: 0000 0101 Result: r = 5 Negative number: r = - 20 > > 2 Binary original code of - 20: 1001 0100 Binary inverse of - 20 : 1110 1011 Binary complement of - 20: 1110 1100 Complement after shifting two digits to the right: 1111 1011 Inverse code: 1111 1010 Original code: 1000 0101 Result: r = -5 | |
< < indicates left shift, regardless of positive and negative numbers, and 0 is added in the low order; | >>Indicates shift to the right. If the number is positive, the high position is supplemented by 0; if it is negative, the high position is supplemented by 1; | >>>It means unsigned right shift, also known as logical right shift, that is, if the number is positive, the high order is supplemented by 0, and if the number is negative, the high order is also supplemented by 0 after right shift |
Any value defined has corresponding binary code at the bottom of storage. Bit operation directly deals with binary code, and the operation efficiency is very high.
Conditional operator
symbol | describe | example |
---|---|---|
?: | Determine which value should be assigned to the variable | a>120? "Right": "wrong" results in "wrong" |
Conditional operator:?: Application example:
public class Dmeo08 { public static void main(String[] args) { int a = 10; String b = a>12?"yes":"wrong";// Formula: x? y:z if x is true, the result is y, otherwise z. System.out.println(b);//Output result: error } }
Formula: x? y:z if x is true, the result is y, otherwise z.
Extensions: String connectors
symbol | describe | example |
---|---|---|
+ | Values on both sides of the join operator | ("" + a+b), get 1020, that is, 10 and 20 connections |
String connector:
public class Dmeo08 { public static void main(String[] args) { int a = 10; int b = 20; //+"Represents a string for the string connector" "" "." System.out.println(""+a+b);//Output result: 1020 System.out.println(a+b+"");//Output result: execute a+b first, so the result is 30 } }
Extensions: shaping operations
public class Demo { public static void main(String[] args) { int a = 120; long b = 1212121222222L; short c = 10; byte d = 5; System.out.println(a+b+c+d); //Output result: 12121222357 the result is of Long type System.out.println(a+c); //Output result: the result is of type int System.out.println(c+d); //Output result: 15. The result is of type int } }
In the above code, the addition result of int, long, short and byte is long by default.
The addition result of int, short and byte is of type int by default.
The addition result of short and byte is int by default.
It can be concluded that in the shaping operation, the result of the operation with long type is long by default, and the result without long type is int by default.
Extension: Java operator priority
category | Operator | Relevance |
---|---|---|
suffix | () [] (point operator) | Left to right |
one yuan | + + - !〜 | Right to left |
Multiplicity | * /% | 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 |