4. Introduction to Java [Operators]

Operators in java can be divided into the following groups:

  • arithmetic operator
  • relational operator
  • bitwise operator
  • Logical Operators
  • assignment operator
  • other operators

1. Arithmetic operators

operator describe example
+ Addition − Adds the values ​​on either side of the operator A + B equals 30
- Subtraction - subtracts right operand from left operand A - B is equal to -10
* Multiply - Multiply the values ​​on either side of the operator A*B is equal to 200
/ Division - left operand divided by right operand B/A is equal to 2
remainder - left operand divided by right operand remainder B%A is equal to 0
++ Increment: the value of the operand is increased by 1 B++ or ++B equals 21 (see below for differences)
Decrement: Decrease the value of the operand by 1 B-- or --B equals 19 (see below for differences)
package com.itfeiniu.hello;

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(5 / 2);              //2
        System.out.println((float) (5 / 2));    //2.0
        System.out.println(5.0 / 2);            //2.5
        System.out.println(5 / 2.0);            //2.5
        System.out.println(5 / 2.0F);           //2.5
        System.out.println('A' / 2);            //32
        System.out.println('A' / 2.0);          //32.5
        System.out.println('a' / 2);            //48
        System.out.println('a' / 2.0);          //48.5

        int a = 1;
        double b = 1.0;
        double c = 1.5;
        System.out.println(++a);        //2
        System.out.println(a++);        //2
        System.out.println(++b);        //2.0
        System.out.println(++c);        //2.5
    }
}

In addition to addition, subtraction, multiplication and division, arithmetic operators also have special operations such as increment and decrement.

Prefix self-increment and self-subtraction (++a,–a): perform self-increment or self-decrement operation first, and then perform expression operation.

Suffix self-increment and self-subtraction method (a++, a–): perform expression operation first, and then perform self-increment or self-decrement operation Example:

2. Relational operators

operator describe example
== Checks if the values ​​of the two operands are equal, if they are equal then the condition becomes true. (A == B) is false.
!= Checks if the values ​​of the two operands are equal, if the values ​​are not equal then the condition becomes true. (A != B) is true.
> Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true. (A > B) is false.
< Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true. (A < B) is true.
>= Checks if the value of the left operand is greater than or equal to the value of the right operand, if so then the condition becomes true. (A >= B) is false.
<= Checks if the value of the left operand is less than or equal to the value of the right operand, if so then the condition becomes true. (A <= B) is true.

3. Bitwise operators

Java defines bitwise operators that apply to integer types (int), long integers (long), short integers (short), character types (char), and byte types (byte).

Bitwise operators operate on all bits and perform bitwise operations. Assuming a = 60,b = 13; their binary format representation will be as follows:

A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
~A= 1100 0011
operator describe example
If the corresponding bits are all 1, the result is 1, otherwise it is 0 (A & B), get 12, which is 0000 1100
| The result is 0 if the corresponding bits are all 0, otherwise 1 (A|B) gets 61 which is 0011 1101
^ If the corresponding bit values ​​are the same, the result is 0, otherwise 1 (A^B) gets 49 which is 0011 0001
The bitwise negation operator flips each bit of the operand i.e. 0 becomes 1 and 1 becomes 0. (~A) gets -61 which is 1100 0011
<< Bitwise left shift operator. The left operand is bitwise shifted left by the number of bits specified by the right operand. A << 2 gets 240 which is 1111 0000
>> Bitwise right shift operator. Bitwise shifts the left operand to the right by the number of bits specified by the right operand. A >> 2 gets 15 which is 1111
>>> Bitwise right shift zero padding operator. The value of the left operand is right-shifted by the number of bits specified by the right operand, and the shifted positions are filled with zeros. A>>>2 gets 15 which is 0000 1111

4. Logical operators

operator describe example
&& Called the logical AND operator. The condition is true if and only if both operands are true. (A && B) is false.
| | Called the logical OR operator. Condition becomes true if either of any two operands is true. (A||B) is true.
Called the logical NOT operator. Used to invert the logical state of an operand. The logical NOT operator will get false if the condition is true. ! (A && B) is true.

5. Assignment operator

operator describe example
= A simple assignment operator that assigns the value of the right operand to the left operand C = A + B will assign the value obtained by A + B to C
+ = The addition and assignment operator, which adds the left operand to the right operand and assigns the value to the left operand C + = A is equivalent to C = C + A
- = Subtraction and assignment operator, which subtracts the left operand from the right operand and assigns the value to the left operand C - = A is equivalent to C = C - A
* = The multiply and assignment operator, which multiplies the left operand by the right operand and assigns the value to the left operand C * = A is equivalent to C = C * A
/ = The division and assignment operator, which divides the left operand by the right operand and assigns the value to the left operand C / = A, C = C / A when C is the same type as A
(%)= 取模和赋值操作符,它把左操作数和右操作数取模后赋值给左操作数 C%= A等价于C = C%A
<< = 左移位赋值运算符 C << = 2等价于C = C << 2
>> = 右移位赋值运算符 C >> = 2等价于C = C >> 2
&= 按位与赋值运算符 C&= 2等价于C = C&2
^ = 按位异或赋值操作符 C ^ = 2等价于C = C ^ 2
| = 按位或赋值操作符 C | = 2等价于C = C | 2

六、运算符优先级

从上到下优先级为由高到低。

类别 操作符 关联性 优先级
后缀 () [] . (点操作符) 左到右
一元 expr++ expr– 从左到右
一元 ++expr --expr + - ~ ! 从右到左
乘性 * /% 左到右
加性 + - 左到右
移位 >> >>> << 左到右
关系 > >= < <= 左到右
相等 == != 左到右
按位与 左到右
按位异或 ^ 左到右
按位或 | 左到右
逻辑与 && 左到右
逻辑或 | | 左到右
条件 ?: 从右到左
赋值 = + = - = * = / =%= >> = << =&= ^ = | = 从右到左
逗号 左到右

Guess you like

Origin blog.csdn.net/bobo789456123/article/details/131776000