JAVA basic (three) operator

The basic operators commonly used in java and the less commonly used operators are sorted and classified

table of Contents

One, basic operators

Unary operator

Binary operator

Ternary operator

Second, the assignment operator

Three, relational operators

Four, bit logic operators

Five, operator precedence


One, basic operators

 There are three basic operations

Unary operator

There are 3 unary arithmetic operations, namely -, ++ and --.

-Negation sign inversion operation b=-a
++ Self-increment one first take the value and then add one, or add one first and then take the value a++ or ++a
- decrement one first take the value and then subtract one, or subtract one and then Take the value a-- or--a


 int a=2;
 int b=-2;
System.out.println(a);//2
System.out.println(-a);//-2
System.out.println(a==-a);//false
System.out.println(b==-a);//true


 

Binary operator

In addition to addition (+), subtraction (-), multiplication (*) and division (\), there are also modulo operations (%).

Addition (+), subtraction (-), multiplication (*), division (\) have the same meaning as the mathematical operations we usually touch

Ternary operator

Represented by the ?: symbol,

The specific meaning is actually similar to the meaning of the if-else structure.

This operator will perform two treatments for a certain condition. If the condition is met, the first result will be executed, and if the condition is not met, the other result will be executed.

int A,B,C; A=2; B=3; C=A>B ? 100 :200;
System.out.println(C);//200

 

Second, the assignment operator

 The priority of assignment operators is lower than that of arithmetic operators, and the combination direction is from right to left;

Not the equal sign in mathematics, it means an action,

That is, the value on the right side is sent to the variable on the left side (only variables are allowed on the left side, not expressions or other forms);

int x, y, z; // 定义3个整型的变量
x = y = z = 5; // 为变量赋初值为5
x += 10; // 等价于x=x+10,结果x=15(加等)
y -= 3; // 等价于y=y-3,结果y=2(减等)
z *= 5; // 等价于z=z*5,结果z=25(乘等)
x /= 4; // 等价于x=x/4,结果x=3(除等)
z %= x; // 等价于z=z%x,结果z=1(模等)

Three, relational operators

Operator meaning Description Instance result
> Greater than operator Only supports that the left and right operands are numeric types. If the value of the preceding variable is greater than the value of the following variable, it returns true. 2>3 false
>= Greater than or equal to operator Only supports that the left and right operands are numeric types. If the value of the preceding variable is greater than or equal to the value of the following variable, it returns true. 4>=2 true
< Less than operator Only supports that the left and right operands are numeric types. If the value of the preceding variable is less than the value of the following variable, it returns true. 2<3 true
<= Less than or equal to operator Only supports that the left and right operands are numeric types. If the value of the preceding variable is less than or equal to the value of the following variable, it returns true. 4<=2 false
== Equality operator If the two operands to be compared are both numeric types, regardless of whether their data types are the same, as long as their values ​​are not equal, it will return true.
If both operands are reference types, they can be compared only when the types of the two reference variables have a parent-child relationship. As long as the two references do not point to the same object, it will return true.
Java  also supports comparison of two boolean values.
4==4
97=='a'
5.0==5
true==false
true
true
true
false
!= Inequality operator If the two operands to be compared are both numeric types, regardless of whether their data types are the same, as long as their values ​​are not equal, it will return true.
If both operands are reference types, they can be compared only when the types of the two reference variables have a parent-child relationship. As long as the two references do not point to the same object, it will return true.
4!=2 true

 

Four, bit logic operators

Bit logical operators, less used

Bit logic operators include 4: & (and), | (or), ~ (not) and ^ (exclusive or).

Except for ~ (i.e. bit negation), which is a unary operator, the rest are all binary operators 

        //按位进行与运算(AND)
        System.out.println(4&5);//4
        //按位进行或运算(OR)
        System.out.println(4|5);//5
      // 按位进行异或运算(XOR)
        System.out.println(4^5);//1
        //按位进行取反运算(NOT)
        System.out.println(~5);//-6

Five, operator precedence

Generally speaking, unary operators have higher precedence, and assignment operators have lower precedence.

Arithmetic operators have higher precedence, and relational and logical operators have lower precedence.

Most operators are left associative,

Unary operators, ternary operators, and assignment operators have right associativity.

 

reference:

http://c.biancheng.net/view/777.html

 

Guess you like

Origin blog.csdn.net/qq_37203082/article/details/102544157