Let’s first explain what operators are

Symbols that operate on variables or constants are called operators
Put together, it’s an expression

### List of articles

• Arithmetic operator
• Incremental assignment operator
• Self increasing / Self – subtracting operator
• Relational operator
• Logical operators
• An operator
• Shift Operators （ Understanding can ）
• Ternary operator
• priority
• notes

# Arithmetic operator

That is the most basic + – * / %
These are relatively simple to use , But should pay attention to / and % The use of

```System.out.println(3/2); No 1.5, It is 1, because int/int What we should get is intSystem.out.println((float)3/2);      1.5System.out.println(3/(float)2);      1.5 As long as one is floating-point, the answer is floating-point
System.out.println((float)(3/2));    1.0 This is the first calculation 1 Re convert to 1.0```

Be careful ： The divisor cannot be zero 0, By this mistake , Let’s take a brief look at Java A description of when something goes wrong , If you’re interested in anomalies , I’ll post a blog about anomalies later . ```System.out.println(10%3);       110-3*3=1System.out.println(10%-3);      110-(-3)*(-3)=1System.out.println(-10%3);      -1-10-(-3)*3=-1System.out.println(-10%-3);     -1-10-(-3)*3=-1 It can be seen from the above formula that the sign of remainder is only related to the sign of the preceding number
System.out.println(12.5/3);      0.5C We can't carry out decimal remainder in language , but java Sure ```

# Incremental assignment operator

namely += -= *= /= %=

```int a=2;a+=1 Equate to a=a+1 however java The incremental assignment operator in can automatically force type conversion
example  short b=1;
b=b+2;// It's a warning ,b+2 After that, it will improve , Type mismatch
b+=a;// In this way, the forced conversion is automatically completed
+= -= *=  /= %= It's called the composition operator , All of them can help us make a cast ```

# Self increasing / Self – subtracting operator

namely ++ 、- –

``` In front of ++  First ++ After use
After ++  after ++ First use  -- Empathy int a=1;int b=a++;b=1,a=2int a=1;int b=++a;b=2,a=2a++; or ++a; This is the moment a+1, There is no difference between ```

# Relational operator

There are six relational operators ：== 、 >= 、 <= 、 > 、 < 、 !=

`int a=1,b=2;System.out.println(a>b);     falseSystem.out.println(a<b);     trueSystem.out.println(a>=b);    falseSystem.out.println(a<=b);    trueSystem.out.println(a!=b);    trueSystem.out.println(a==b);    false`

These expressions in brackets are Boolean expressions , The result is either true, Or false

# Logical operators

&& It’s called logic and （ Or short circuit with ）,|| It’s called logic or ( Or short circuit or ),！ It’s called logical non

``` Boolean expression 1 &&  Boolean expression 2 As long as there's an expression for false, The whole expression is false If the expression 1 For false , The expression is no longer executed 2 example  System.out.println(1>2 && 1/0); That was already said 0 Divisor will report an error , Will it report an error now ？
Can't , because 1>2 For false , Then it won't happen again 1/0 This expression has been changed , All that's printed on the screen is false```

``` Boolean expression 1 ||  Boolean expression 2 (C Expressions in languages can be of other types ) As long as there's an expression for true, The whole expression is true If the expression 1 It's true , The expression is no longer executed 2 example  System.out.println(1<2 && 1/0); What about the result ？
Same as above, because 1<2 It's true , Then it won't happen again 1/0 This expression has been changed , All that's printed on the screen is true```

`! Still combined with Boolean expressions !true == false!false == true`

C Expressions in languages can be of other types ,Java It doesn’t matter 0 For false , Non zero is true

Not recommended &（ And ） and |（ or ）, Because they have to be executed on both sides , Short circuit evaluation is not supported

`System.out.println(1>2 & 1/0);System.out.println(1<2 | 1/0); Both of these will be abnormal `

# An operator

There are four bit operators ：& | ^ ~

Bit operation means binary bit operation , All computers use binary to represent data （01 Sequence of composition ）, Bitwise operation is in accordance with each bit of the binary bit in order to calculate

``` Bitwise AND &：   0000 1011
0000 0101 result    0000 0001 The corresponding bits are 1 It's just 1, The rest are 0```

``` Press bit or  |：   0000 1011
0000 0101 result    0000 1111 The corresponding bits are 0 It's just 0, The rest are 1```

``` Bitwise XOR  ^：   0000 1011
0000 0101 result    0000 1110 The corresponding bit is different 1, Same as 0```

` According to the not  ~：   0000 1011  result    1111 01000 become 1,1 become 0`

# Shift Operators （ Understanding can ）

There are three shift operators ：<< 、>> 、>>>( Use… For complement )

` Shift left operator <<   It's like multiplication , move n position , The original number is multiplied by 2 Of n Power ( The right to repair 0)0000 0110 <<1   6        0000 0110 <<2   60000 1100       12       0001 1000       24`

` Shift right operator >>   It's equivalent to division , move n position , Divide the original number by 2 Of n Power ( Fill in the sign on the left )0000 0110 >>1   6        0000 0110 >>2   60000 0011       3        0000 0001       1`

Be careful ： Shift right operator pairs -1 of no avail (-1 The complement of is 32 individual 1, Moving right makes up for 1, Or -1)

` unsigned right shift >>>  Negative or not , Fill all the left 0-1>>>1     Move right and fill left 0, Become a very large positive number （ Binary for 0 Back 31 individual 1）`

Be careful ： There is no unsigned shift left operator
Because the efficiency of computer calculation is higher than that of multiplication and division , When a code just multiplies and divides 2 Of n We can use shift operation instead of .

# Ternary operator

Boolean expression ? expression 1： expression 2
Perform logical ： If the Boolean expression is true , Then execute the expression 1; If the Boolean expression is false , Then execute the expression 2

`int a=1;int b=2;int max=a>b?a:b; You can nest int c=3;max=c>(a>b?a:b)?c:a>b?a:b;`

# priority

There is priority between operators , No need to remember , Just put parentheses where there may be ambiguity # notes

Generally used in front of methods and classes ``` Multiline comment /*
`// int a=0;`