1, Arithmetic operator
(1) Basic four operators: + - * / (Division)% (remainder)
- Precautions for "/"
public class TestDemo { public static void main (String[] args) { System.out.println(5/2);//The result of int/int is still int, so use double System.out.println(5.0/2);//double operation System.out.println((float)5/2);//First convert 5 to float and divide System.out.println((float)(5/2);//Force int result to float } }
Note: 1. %Not only can int Modulus (remainder) can also be used for double Perform modulo (remainder) 2. 0 You can't divide
System.out.println(10 / 0);//Execution result: an exception occurs, as shown in the following figure
- Precautions related to "%" (pay attention to the division of negative numbers)
System.out.println(10 % 3);//Execution result: 1 System.out.println(-10 % 3);//Execution result: - 1 System.out.println(10 % -3);//Execution result: 1 System.out.println(-10 % -3);//Execution result: - 1
(2) Incremental assignment operator (compound operator): + = - = * = / =%=
eg: a += 10; Represents a = a + 10;
(3) Auto increment / Auto decrement operator: + + --
int a = 10; int b = ++a;//Implementation result: 11 System.out.println(b); //First a=a+1, then c=a int a = 10; int c = a++;//Execution result: 10 System.out.println(c); //First c=a, then a=a+1 int a = 10; int b = ++a;//Implementation result: 11 System.out.println(b); int c = a++;//Implementation result: 11 System.out.println(c); //When a is assigned to c, it is assigned to c when the value of a is 11 due to the above + + A; After printing c, a increases from 12 t to 12 t int i = 10; i = i++;//Execution result: 10 System.out.println(i); //As for why it is equal to 10, you can learn the disassembly instructions of java, which is more complex
Precautions: 1.If not taken from the increase/If the return value of the expression of the self subtraction operation, the preceding self increment/Self decreasing and post self increasing/Self subtraction makes no difference 2.If the return value of the expression is taken, the leading return value is self increment/The value after self subtraction, and the post return value is the value before self increment
2, Relational operator
(1) There are six main types: = =! = < > < = >=
be careful:The expression return values of relational operators are boolean type
3, Logical operator
(1) Logic and & & (both expressions are true, the result is true, otherwise the result is false)
(2) Logical or 𞓜 (both expressions are false, the result is false, otherwise the result is true)
(3) Logical non! (the expression is true, the result is false, the expression is false, the result is true, and the unary operand has only one expression, which can only act on boolean data)
(4) Short circuit evaluation
- &&Once expression 1 is false, expression 2 will not be executed
- ||Once expression 1 is true, expression 2 will not be executed
System.out.println(10>20 && 10/0 == 0);//Execution result: false System.out.println(10<20 || 10/0 == 0);//Execution result: true //10 / 0 does not report an error because neither code executes the second expression (short-circuit evaluation)
Note: logical operator operands (often the result of relational operators) and return values are boolean type
4, Bitwise operator
(1) The smallest unit of data storage in java is bytes
(2) The smallest unit of data operation in java is binary bit
(3) There are four bit operators: & (bitwise AND) | (bitwise OR) ~ (bitwise negation) ^ (bitwise XOR)
(4) Take 13 (0000 1101) and 11 (0000 1011) as examples to participate in binary operation
- Bitwise AND & calculation result: 0000 1001 (write the same number and write 0 differently)
- Calculation result by bit or | 0000 1111 (the same number corresponds to write, but not write 1)
- Calculation result by bit XOR ^ 0000 0110 (write 0 for the same number and 1 for different numbers)
- Bitwise inversion ~ take the number 11 as an example: 11110100 (write 0 as 1 and 1 as 0)
5, Shift operator
(1) Shift left < < (shift left to the right to supplement 0, and shift left is equivalent to multiplication)
eg: 11<<1(11(0000 1011)Shift left by 1 bit) and the result is 0001 0110 (22) = 11*2) 11<<2,The result is 0010 1100 (44 = 11*4)
(2) Shift right > > (shift right to fill the sign digit number to the left, and shift right is equivalent to Division)
eg: 11>>1,The positive complement sign bit is 0, and the result is 0000 0101 (5 = 11/2^1) 11>>2,The result is 0000 0010 (2 = 11/2^2) -1(1111 1111)>>1,The negative complement sign bit is 1, and the result is 1111
(3) Move right without sign > > > (just fill in 0)
6, Conditional operator (ternary operator)
There is only one: expression 1? Expression 2: expression 3
(expression 1 is true, and the value of the whole expression is the value of expression 2; expression 1 is false, and the value of the whole expression is the value of expression 3)
//Find the maximum of two integers int a = 10; int b = 20; int max = a>b?a:b;//Implementation result: 20 System.out.println(max);
7, Operator priority
You don't have to remember. You can add parentheses to ambiguous codes
8, Binary problem
Binary (consisting of 0 and 1) octal (consisting of 0)~7 Decimal (consisting of 0)~9 Composition) Hex (by 0)~15 Composition: within 10, use numbers to indicate more than 10 a~f Representation (both case and case)
(1) Decimal to binary (generally not required)
Method 1: eg1: Find the binary of 10: add from the power of 2 close to 10. The first is 8 and the second is 2, so 10=8+2 Corresponding to the following binary lattice: 0000 1010, 8 And 2 are in the second and fourth bits respectively, so remove the second and fourth bits and supplement 0 in other positions. eg2: Find the binary of 75: the first one close to 75 is 64, The second is close to 75-64=11 The power of 2 is 8; And so on, the third is 2 and the fourth is 1 Therefore, the binary of 75 corresponds to the following grid: 0100 1011
Method 2: rolling phase division eg: Find the binary of 10 (as shown in the figure below)
(2) Binary to decimal
eg1:Find the decimal system of 0000 1010: 0000 1010 = 1*2^1+1*2^3 = 10 I.e. each digit*Hexadecimal number^This bit weight can be added after each bit is multiplied eg2: Find the decimal system of 0100 1011: 0100 1011 = 1*2^0+1*2^1+1*2^3+1*2^6 = 75
(3) Binary to octal
eg: Find octal of 130, then: According to the rolling division method, the binary of 130 is 010 000 010 =1000 0010(To octal (binary 3 bits are often regarded as one) Take 010 as a binary, representing 2; 000 represents 0; 010 represents 2; I.e. 202→Correct writing: 0202 (octal starts with 0)
(4) Binary to hexadecimal
eg: Find the hex of 130, then: Binary: 1000 0010 (binary four bits are often regarded as one when converting to hexadecimal) Regard 1000 as a binary, representing 8; 0010 represents 2; Namely: 0 x0000 0082→0x82(Hexadecimal 0 x (beginning)
(5) Hex to octal (first binary, then octal)
(5) Binary for negative numbers
eg: First take the original code, get the inverse code, and finally get the complement=Inverse code+1,The complement is the result