Operation symbols, arithmetic operators, assignment operators, comparison (relational) operators, logical operators, bitwise operators, conditional operators, operator precedence

Table of contents

1. Arithmetic operators

2. Assignment operator

3. Comparison (relational) operators

4. Logical operators

5. Bit operators

6. Conditional operators

7. Operator precedence


An operator is a special symbol used to represent data operations, assignments, comparisons, etc.

Classification of operators: According to their functions, they are divided into: arithmetic operators, assignment operators, comparison (or relational) operators, logical operators, bitwise operators, conditional operators, and Lambda operators.

According to the number of operands, they are divided into: unary operators (unary operators), binary operators (binary operators), and ternary operators (ternary operators).

1. Arithmetic operators

2. Assignment operator

Basic syntax: The symbol is: =

①: When the data types on both sides of '=' are inconsistent, you can use automatic type conversion or the principle of forced type conversion to handle it.

②: Support continuous assignment

Extended assignment operators: +=, -=, *=, /=, %=

3. Comparison (relational) operators

Notice:

①: The results of comparison operators are all boolean, that is, the result is either true or false.

②: >, <, >=, <= only apply to basic data types (except boolean types)

③: ==, != applies to basic data types and reference data types

④: The comparison operator "==" cannot be mistakenly written as "="

4. Logical operators

Basic syntax:

Logical operators operate on boolean type variables or constants, and the result of the operation is also a boolean type value.

Operator description:

①: & and &&: represents the relationship of "and". When the Boolean values ​​on the left and right sides of the symbol are both true, the result can be true, otherwise, it is false.

②: | and ||: represent the "or" relationship. When one of the Boolean values ​​on both sides of the symbol is true, the result is true. When both sides are false, the result is false

③:!: Indicates a "not" relationship. When the Boolean value of the variable is true, the result is false. When the variable Boolean value is false, the result is true

④: ^: When the Boolean values ​​on the left and right sides of the symbol are different, the result is true. When the Boolean values ​​on both sides are the same, the result is false. Understand: XOR, what is pursued is "extraordinary"!

Notice:

①: Logical operators are used to connect Boolean expressions. They cannot be written as 3<x<6 in Java, but should be written as x>3&x<6

②: Distinguish between "&" and "&&":

Similarity:: If the left side of the symbol is true, both perform the operation on the right side of the symbol.

The difference: for &, if the left side of the symbol is false, the operation on the right side of the symbol will continue; for &&, if the left side of the symbol is false, the operation on the right side of the symbol will not continue.

Suggestion: Under development, it is recommended to use &&

③: Distinguish between "|" and "||":

Similarity: If the left side of the symbol is false, both perform the operation on the right side of the symbol.

Difference: For |, if the left side of the symbol is true, the operation on the right side of the symbol will continue. For ||, if the left side of the symbol is true, the operation on the right side of the symbol will not continue.

Recommendation: Under development, recommended to use ||

5. Bit operators

The operation process of bit operators is based on binary complement operation.

①Move left: <<

Operation rules: Within a certain range, each time the data moves one bit to the left, it is equivalent to the original data * 2. (Applicable to both positive and negative numbers)

Note: When the number of bits shifted to the left exceeds the total number of bits of the data type, it is equivalent to a left shift of (n-total number of bits) bits.

②Move right: >>

Operation rules: Within a certain range, each time the data moves one bit to the right, it is equivalent to the original data/2. (Applicable to both positive and negative numbers)

Note: If it is not divisible, round down.

③: Unsigned right shift: >>>

Operation rules: After moving to the right, the vacated bits on the left are directly filled with 0. (Applicable to both positive and negative numbers)

④: Bitwise AND: &

Operation rules: The corresponding bit is 1 only if it is 1, otherwise it is 0

⑤: Bitwise OR: |

Operation rules: As long as the corresponding bit has 1, it is 1, otherwise it is 0

⑥: Bitwise XOR: ^

Operation rules: If the corresponding bit is 1 and the other is 0, it is 1, otherwise it is 0

⑦: Bitwise negation: ~

Operation rules: If the corresponding bit is 1, the result is 0; if the corresponding bit is 0, the result is 1

6. Conditional operators

Basic syntax:

Conditional operator format:

(Conditional expression)?Expression1:Expression2

Description: The conditional expression is a result of boolean type. Expression 1 or expression 2 is selected based on the boolean value.

Conversion relationship with if-else:

Wherever conditional operators can be used, they can be rewritten as if-else structures. On the contrary, it is not true.

During development, if both can be used, it is recommended to use the conditional operator because its execution efficiency is higher

7. Operator precedence

Operators have different priorities. The so-called priority is the order of operators in the expression operation.

The operator in the previous line is always better than the one in the next line

Suggestions for actual development:

①Don’t rely too much on the priority of operations to control the execution order of expressions. This will make the readability poor. Try to use () to control the execution order of expressions.

②Don’t write an expression that is too complex. If an expression is too complex, divide it into several steps to complete.

Guess you like

Origin blog.csdn.net/Starinfo/article/details/135141734