# 1, Operator

​ In Java, operators used for program calculation are generally called operators, which are divided into the following categories

## 1. Arithmetic operator

operator explain
+ Both sides of the plus sign are numeric values, which can be calculated. If there is a string on one side, it will be regarded as a connector a+b
- When two numbers are added together, the minus sign can also represent a minus sign a-b,-a
* Multiply two numbers, where * cannot be omitted a*b
/ When dividing two numbers, the data type must be consistent, and the divisor cannot be 0, otherwise an arithmetic exception occurs a*b
% Take the remainder of a number a%b
++ Self addition to a number a++,++a
-- Self subtraction of a number a--,--a

//Summary + + -- has higher priority than +, -, *, /,%

```public static void main(String[] args){
//+,-,*,/,% 	 All operators must ensure consistent data types during calculation
int num1 = 100;
int num2 = 200;
int sum = num1 + num2;
int mul =  num1-num2;
int num3 = -num2;   // -200
System.out.println("Add two numbers" +  sum); // +Represents a connector
System.out.println("Subtract two numbers" + mul);
System.out.println( "num2: " + num2+ " ,num3:" + num3 );
System.out.println("num1+num2="+ (num1+num2) );

int sum2 = num1*num2;
int sum3 = num1/num3;
System.out.println(sum3);
// System.out.println(num1/sum3); // Arithmetic exception: / by zero
// Take mold
System.out.println( 10%2);//0
System.out.println(1%5);// 1
System.out.println(2%5);
}
```
```public static void main(String[] args) {
// Self addition and self subtraction + + -- only integers can be added
int i=1;
i++ ;     // i = i + 1
System.out.println("i:" + i);
int j=1;
++j;    // j = j + 1
System.out.println("j:" + j);

int a =1;
int sum = a++;  // First assign the value of a to sum, and then add 1 to a
int sum2 = ++a; // First add a by itself, and then assign the result of self addition to sum2
System.out.println("sum:" + sum);
System.out.println("sum2:" + sum2 );
System.out.println("a:" +a);  // 3

int sum3 = a++ + a++ ;
System.out.println("sum3:"+sum3);
System.out.println("a:"+a);

// --The operation is similar to + + operation
int b=1;
int s1= b--;  // The value of B = B - 1 S1 is assigned first and then subtracted by 1 S1 = 1
int s2 =  --b; // B = B - 1. The value of S2 is to subtract 1 first and then assign s2 = -1
System.out.println("b:"+b);
System.out.println("s1:"+s1);
System.out.println("s2:"+s2);

// ++-- Comprehensive
int x=1;
int y=2;
int s3 = x++ + --x * (y-- + ++x);
System.out.println("x:"+x);// 2
System.out.println("y: "+y);// 1
System.out.println("s3:"+s3);// 5
}
```

## 2. Comparison operator

Used to compare the results between two expressions. The results return true and false

The comparison operator cannot be run as a single line of code. It must receive the result or output

Comparison operator explain case
> Compare whether the number on the left is greater than the number on the right. If greater than, return true; otherwise, return false a>b ,3>5
< Compare whether the number on the left is less than the number on the right. If it is greater than, return true; otherwise, return false a<b
>= Compare whether the number on the left is greater than or equal to the number on the right. If greater than, return true; otherwise, return false 1>=2
<= Compare whether the number on the left is less than or equal to the number on the right. If it is greater than, return true; otherwise, return false 1<=2
== Compare = = whether the two times are equal. If they are equal, return true; otherwise, return false. Compare whether the values are equal for the basic data type and whether their addresses are equal for the reference data type (the comparison address is to compare whether they are the same block of memory) 1==2
!= In contrast to = =, compare whether the numbers on the left and right are not equal. Returns true if not equal and false if equal 1!=2

Note: it is not allowed to write together, for example, 1 < 3 < 5

```public static void main(String[] args) {
// Comparison operator
int a =1;
int b =2;
// a>b;  //  Cannot be compared alone. Results or outputs must be accepted
System.out.println(a>b);  //false
System.out.println(a<b);  //true
System.out.println(a==b); //false
System.out.println(a!=b); //true
// If the condition after if is true, execute the statement in if. If not, just use else statement
if(a>b){
System.out.println("a>b establish");
}else{
System.out.println("Not established");
}

}
```

## 3. Assignment operator

Assign the result of the expression to a variable, which can only be assigned to variables, not constants

For example: a = 3

Assignment Operators explain case
= Assign the result of = right to the variable on the left int a = 2, assign 2 to variable a
+= After calculation, assign the result of + = right to the variable on the left a+=2 is equivalent to a = a +2
-= Assign the value after calculation, and assign the result of - = right to the variable on the left a-=b is equivalent to a = a-b
*= As above, assign the result on the right to the variable on the left a*=b is equivalent to a = a * b
/= As above, assign the result on the right to the variable on the left a /=b is equivalent to a = a/b, where b= 0
%= As above, assign the result on the right to the variable on the left A% = b equals a= a%b where b= 0
```// Assignment operator = + = - = * = / =%=
int a=2;
a+=2;
System.out.println(a);// 4
a-=3;
System.out.println(a);// 1
a*=2;
System.out.println(a); // 2
a/=4;
System.out.println(a);// 0

a+=a-=3; // a+=(a-=3) -> a=a +(a=a-3 )
System.out.println("a="+a);

int x=2;
x+=x-=x*=x++;
//x = x +(x = x -( x= x *(x++) ))
// x = 2 + ( x = 2 - (x = 2 * 2))
// x =  2 + ( 2 - 4)
// x = 0
System.out.println("x="+x);

//The assignment operator has the lowest priority and is calculated from right to left
int y=2;
y*=y+=y; // It is also calculated from right to left
// y = y * (y = y + y);
// y = 2 * (2+2)
// y =8;
System.out.println("y="+y);
```

## 4. Logical operator

In Java, it is used for two or more expressions to take the result of logical judgment. Logical operators are usually required

Logical operator explain case
& Logical and, the left and right sides can connect expressions. When both expressions are true, the whole result is true true&true, false&true false&false , true&false
| Logical or, either of the left and right expressions holds, and the whole result is true true|true false|true true|false false|false
! Logical negation of expression !false !true
&& Short circuit and, similar to &, short circuit features: false on the left and no operation on the right true&&true true&&false ..
|| Paragraph or, similar to |, paragraph features: the left side of the symbol is true, and the right side is no longer calculated false||true false||false
```  // Logical operator
System.out.println(true & true); // true
System.out.println(true & false);// false
System.out.println(false & true); // false
System.out.println(false & false);// false
// true&true
System.out.println(1>0 & 3>1);
System.out.println(1>0 && 3>1);
// |Or
System.out.println(true | true); //true
System.out.println(true | false);// true
System.out.println(false | true); // true
System.out.println(false | false);// false
// ||Short circuit or
System.out.println(true || false) ;// true
```

Summary: & the difference between & & and | the difference between | and |?

Answer 1, & the expressions on both sides of the symbol will be executed & & if the left side of the symbol is false, the right side will not be executed

​ | The expressions on both sides of the symbol will be executed. If the left side of the symbol is true, the right side will not be executed

2. Numbers can be written directly on both sides of &, | and calculated by bit. Short circuit symbols cannot directly calculate numbers

```int a=1;
int b=2;
//   System. out. println(a>b && b++>0);  //  No operation to the right of the symbol
//   System.out.println("b:"+b);

System.out.println(a>b & b++>0); // Run on both sides of the symbol
System.out.println("b:"+b);

// ||The difference between and
//  System. out. println(a>=1 ||  a++<0); //  A + + does not execute
//  System.out.println("a:"+a);

System.out.println(a>=1 | a++<0 );  // a + + will execute
System.out.println("Output again a : " + a);
```

### Decimal to binary

decimal system transformation Binary
1 0 power of 1 * 2 1
2 1 * 2 to the power of 1 10
4 Power of 1 * 2 100
For multiples of 2, the n-th power of 1 * 2 is converted to n zeros after binary bit 1
16 The fourth power of 1 * 2 10000

For any decimal to binary conversion, first find the multiple of 2 that is smaller than it and closest to it, and then calculate the binary difference (using the split method)

For example, 28 = 16 + 8 + 4 = 11100

​ 34 = 32+2 = 100010

​ 96 = 64+32 = 1100000

### Binary to decimal

Formula: starting from bits, the sum of the number on each bit multiplied by the n-th power of 2

100100 = power 2 of 1 * 2 + power 5 of 1 * 2 = 36

## 5. Bitwise operator

Shift Operators explain case
<< Shift left: after converting a number into binary, move the whole number to the left by N bits, expand the multiple, and move a number to the left by N bits. The result is the number multiplied by the n power of 2 3 < < 2 = the second power of 3 * 2
>> Shift right: after a number is converted into binary, the whole number moves to the right by N bits, and the multiple is reduced. A number moves to the right by N bits. The result is the number divided by the n-th power of 2 (except not considered separately) 8> > 2 = the power of 8 / 2
> > > Move the unsigned right, regardless of the sign bit, and move the whole number n bits to the right.
^n XOR
~n Numerical inversion
```//Shift of positive integer < < > > >
System.out.println(3<<2);  // 12
System.out.println(7<<3); // 7*8=56

// For positive numbers, the unsigned right shift results in the same way as the right shift
System.out.println(16>>2);// 4
System.out.println(18>>2);// 4

System.out.println(5^9);//12

System.out.println(3>>2);
System.out.println(3>>>2);

System.out.println(~5);

```
```// Shift of negative numbers
// Is the left shift of a negative number negative
System.out.println(-4<<2); // -4 * 2 to the power of 2=
/**
*   -4 Original code: 10000 00000100
*          *   -4 Inverse code: 1 1111 eleven million one hundred and eleven thousand and eleven
*          *   -4 Complement of: 1 1111 eleven million one hundred and eleven thousand and one hundred
*          *   Start shift 2
*             1 1111... 11110000
*    Final result = negative + 1
*             1 0000... 00001111  + 1
*  :
*             1 0000... 00010000  =-16
*/

//Complement = inverse + 1
//Negative numbers shift the complement - 4 / 2 = - 2
System.out.println(-4>>1);

// -16 unsigned shift right 2 bits
System.out.println(-16>>>2);   //1073741820
// -Calculate the complement of 16
/**
* Original code 10000 0010000
* Inverse code 11111 one million one hundred and one thousand one hundred and eleven
* Complement 11111 one million one hundred and ten thousand
*      00111.. 1111100  Since the sign does not move, it becomes a positive number after all the high numbers are added
*      If the original code of a positive number is consistent with the complement, this number is the result
*      1073741820
*/
```

## 6. Ternary operator

​ expression? Result 1: result 2

When? When the previous is true, the whole expression outputs the result 1. If? If the previous is not true, output result 2

```// Generate random numbers within 100
int n = (int)(Math.random()*100);
System.out.println("n:"+n);
System.out.println( n%2==0 ?"This number is even":"This number is odd");
```

# 2, Expression, statement block

​ Expression: an element composed of operators and operands. The expression cannot be written in the program alone. It needs to accept the result or output.

There may be multiple operators in the expression at the same time. At this time, the priority of operators needs to be considered Note: the () here represents the bracket of the method, the dot represents the decimal point, and [] represents the subscript of the array

2-5: used to get the result value

6-12: used to calculate true and false

13,14: Yes assignment. The assignment has the lowest priority

Statement block:

In a method, {} can be used to represent a statement block, or it can be placed outside the method and inside the class, which is called an independent code block

``` public static void main(String[] args) {
//Define a code block in a method
{
// A local variable can only be used in its region
int a=1;
a++;
System.out.println("This is the code block in a method "+a);
}
// a++;
if(true){
System.out.println("This is a if Code block");
}
}
```

Posted by F1Fan on Sat, 14 May 2022 10:05:00 +0300