Java operator.

Java operators are functionally divided into arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and conditional operators.

1. Arithmetic operators

Arithmetic operators include the usual addition (+), subtraction (-), multiplication (*), division (/), modulus (%) to complete integer and floating point data arithmetic operations. In many languages, modulo operations can only be applied to integers. Java extends this to allow modulo operations on floating-point numbers. For example, 3%2 gives a result of 1 and 15.2%5 gives a result of 0.2. The modulo operation can also be used for negative numbers, and the result has the same sign as the first operand, for example, 5 percent -3 is 2, and -5 percent 3 is -2.

In addition, there are two kinds of arithmetic operators, “++” and “–“, called the plus and minus 1 operators respectively. These two operators have prefix and suffix forms, which contain different contents. For example, I ++ and ++ I are executed in a different order, with I ++ followed by +1 and ++ I followed by +1. Similarly for I — and — I. Example:

int i = 1;
 System.out.println(i++); // Output 1 here
 int i = 1;
 System.out.println(++i); // Output 2 here
 int i = 1;
 System.out.println(i--); // Output 1 here
 int i = 1;
 System.out.println(--i); // Output 0 here
Copy the code

2. Relational operators

Relational operators are used to compare two values, including greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), equal to (==), and not equal to (! =) 6 kinds. Relational operators are binary operators, that is, each operator has two operands and the result of the operation is a logical value. Java allows “==” and “! The = “operators are used for any data type. For example, you can determine whether the values of two numbers are equal or whether instances of objects or arrays are equal. An instance is judged by comparing the memory reference addresses of two objects.

3. Logical operators

Logical operators including logic and (&), logic, or (|) and logic (!) . The first two are binary operators and the last is unary. Java provides a “short-circuit” function for logic and and logic. That is, during an operation, the value of the expression to the left of the operator is calculated first. If the value can be used to obtain the value of the entire expression, the calculation of the expression to the right of the operator is skipped.

// the result of an expression is false, which returns false; Logic or | | is the result of an expression is true, the result will return true
public class OperatorsTest {
    public static void main(String[] args) {
        // f1 will not be called
        if ( false && f1() ) {
            System.out.println("Formula One executes.");
        }
        // f2 will not be called
        if ( true || f2() ){
            System.out.println("F2 executed"); }}static boolean f1(a) {
        System.out.println("F1 is called.");
        return true;
    }

    static boolean f2(a) {
        System.out.println("F2 is called");
        return false; }}Copy the code

4. Bit operators

Operators to manipulate bits, including bitwise invert (~) and bitwise and (&), bitwise or (|), or (^), moves to the right (> >), left (< <) and the unsigned right shift (> > >). Bitwise operators operate only on integer and character data.

4.1. Invert (~)

A piece of data that participates in an operation and is “inverted” by binary bits.

Operation rule: ~1=0; ~ 0 = 1;

That is, a binary number is reversed by bits, that is, 0 becomes 1, and 1 becomes 0.

4.2. Bitwise and (&)

The two data in the operation are “and” in binary bits.

Operation rules: 0&0=0; 1 = 0 0 &; 1 & 0 = 0; 1 &1 = 1; That is, only when both digits are 1, the result is 1; otherwise, the result is 0.

For example: 3&5 is 0000 0011&0000 0101 = 0000 0001 therefore, 3&5 is worth 1.

4.3. Bitwise or (|)

Two objects that participate in the operation are “or” in binary bits.

Algorithm: 0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1;

That is, as long as one of the two objects in the operation is 1, its value is 1.

For example: 3 | 5, 0101 = 0000, 0111 or 0000 0011 | 0000, therefore, 3 | 5 to 7.

4.4. Xor (^)

The two data in the operation are xOR in binary bits.

Operation rule: 0^0=0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0;

That is, if two corresponding bits of the two objects in the operation are “different” (different values), the result of that bit is 1, otherwise it is 0.

For example: 3 ^ 5, 0101 = 0000, 0110 or 0000 0011 ^ 0000, therefore, is worth 6 3 | 5.

4.5. Left shift (<<)

Operation rules: according to the binary form of all numbers to the left of the corresponding digit, high displacement (abandoned), low vacancy fill zero.

For example, if 12345 << 1, move the number 12345 one bit to the left:

The decimal value is 24690, which is exactly twice 12345, so you can sometimes use the left shift operator instead of multiplying by 2.

4.6. Move right (>>)

Again, the number 12345, shifted right 1 bit: 12345>>1.

The right shift yields 6172, the same value as the integer 12345 divided by 2, so it is sometimes used instead of dividing by 2.

5. Assignment operators

The assignment operator assigns the value of a constant, variable, or expression to a variable.

Except for “=”, the other operators are special assignment operators. In the case of “+=”, x+ = 3 is the same as x = x+3. It adds x+3 first and assigns the result to the variable x. The -=, *=, /=, %= assignment operators can all be deduced in the same way

6. Conditional operators

The conditional operator (? 🙂 also called “ternary operator” or “ternary operator”.

Syntax: Boolean expressions? Expression 1: expression 2.

Operation: Returns the value of expression 1 if the Boolean expression is true, otherwise returns the value of expression 2.

The precedence of the operator

When evaluating an expression, if there are more than one operator in the expression, the order of precedence of the operators is adjusted from high to low at one time. The precedence of the operators is as follows: