The road of reverse Cultivation -- the operator of Java

1, Operator overview

Operators are operations on operands. There are many Java operators that make up an expression (what are operands and operators, such as 1 + 2, where 1 and 2 are operands, + is an operator, and operators and operands combine to form an expression).

Operators are divided according to the number of operands they require, including unary operators (1 operand), binocular operators (2 operands) and ternary operators (3 operands).

Operators are divided according to their functions, including arithmetic operators, assignment operators, relational operators, logical operators, bit operators, conditional operators, string connection operators and other operators. Common operators are as follows:

  2, Arithmetic operator

Look at the following code (note the comments in the code):

public class ArithmeticOperatorTest {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        System.out.println(a + b); //13
        System.out.println(a - b); //7
        System.out.println(a * b); //30
        /*
         * In the java language, the result of 10 divided by 3 is not 3.3333
         * Because there is a syntax rule in java: int type and int type
         * The final result is int, so 10 divided by 3
         * The result is 3. This process will round off the digits.
         */
        System.out.println(a / b); //3
        /*
         * 10 Find the remainder of 3. Multiply 3 by 3 to get 9 and remainder 1.
        */
        System.out.println(a % b); //1
        /*
         * ++And -- are unary operators, + + is responsible for adding 1 and, - is responsible for subtracting 1
         * The following program focuses on the + + operator, which can be operated according to + +
         * And so on.
         */
        int i = 10;
        i++;
        System.out.println("i = " + i); //11
        int j = 10;
        ++j;
        System.out.println("j = " + j); //11
        /*
         * According to the above program, it can be concluded that + + can appear in front of variables or
         * It can appear after the variable, either before or after the variable, as long as the execution is completed
         * ++,The final result will increase the variable by 1. Then + + appears in front of the variable
         * What's the difference between and after variables? Please see the following procedure
         */
        int k = 10;
        int m = k++;
        /*
         * According to the above conclusion, + + can be executed as long as it appears before or after the variable
         * The variable must be self incremented by 1, so the K variable must be self incremented by 1, so the following output k=11
         */
        System.out.println("k = " + k); //11
        /*
         * Above int m = k + +; The + + and = operators exist simultaneously in the code,
         * So which of these two operators has the highest priority? This is the case in java syntax
         * It is specified that when the + + operator appears in the variable, the assignment operation will be performed first, and then
         * Add 1, so the last m = 10
         */
        System.out.println("m = " + m); //10
        int x = 10;
        int y = ++x;
        //After the uplink code is executed, we can confirm that the X variable must be self incremented by 1, so x=11
        System.out.println("x = " + x); //11
        /*
         * java The syntax also stipulates that + + will add 1 before the variable
         * Then do the assignment operation, so y = 11
         */
        System.out.println("y = " + y); //11
    }
}

3, Relational operator

Look at a piece of code (note the comments in the code):

class RelationOperatorTest {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;
        System.out.println(a > b);//false
        System.out.println(a >= b);//true
        System.out.println(a < b);//false
        System.out.println(a <= b);//true
        System.out.println(a == b);//true
        System.out.println(a != b);//false
        /*
         * "= =" cannot be used to compare whether two strings are equal,
         * The equals method must be called, and the equals method will be called later
         * Explain, now remember
         */
        System.out.println("abc".equals("abc"));//true
    }

}

4, Logical operator

Logical operators mainly include logical and (&), logical or (|), logical XOR (^), short circuit and (& &), short circuit or (|). The characteristic of all logical operators is that the operands are Boolean, and the final operation result is also Boolean. The basic operation rules of logical operators are shown in the following table:  

5, Assignment operator

At present, assignment operators only need to master =, + =, - =, * =, / =,% =. Other binary related contents will be learned in detail later. The operators of assignment class include basic assignment operators (=) and extended assignment operators (+ =, - =, * =, / =,% =). Let's look at a piece of code:

public class AssignmentOperatorTest {
    public static void main(String[] args) {
//Basic assignment operator
        int i;
        i = 10;
        System.out.println("i = " + i);
        i = 100;
        System.out.println("i = " + i);
//Extended assignment operator
        int x = 10;
        x += 1; //Equivalent to x = x + 1
        System.out.println("x = " + x); //11
        int y = 10;
        y -= 1; //Equivalent to y = y - 1
        System.out.println("y = " + y); //9
        int z = 10;
        z *= 2; //Equivalent to z = z * 2
        System.out.println("z = " + z); //20
        int m = 10;
        m /= 3; //Equivalent to m = m / 3
        System.out.println("m = " + m); //3
        int n = 10;
        n %= 3; //Equivalent to n = n% 3
        System.out.println("n = " + n); //1
    }
}

6, Conditional operator

The conditional operator belongs to the ternary operator. Its syntax structure is: Boolean expression? Expression 1: expression 2. Its operation principle is as follows: first judge whether the result of Boolean expression is true or false. If it is true, select the result of expression 1 as the result of the whole expression; otherwise, select the result of expression 2 as the result of the whole expression. Let's look at a piece of code:

public class ConditionalOperatorTest {
    public static void main(String[] args) {
        //Compilation error: This is not a statement
        //10;
        boolean flag = true;
        //Compilation error: This is not a statement
        //flag ? 1 : 0;
        //If the above is a complete statement, it should be written like this
        int k = flag ? 1 : 0;
        System.out.println("k = " + k); //1
        //The most classic usage of ternary operator
        boolean sex = true;
        //When the boolean variable sex is true, the result is' male ', otherwise' female '
        char gender = sex ? 'male' : 'female';
        System.out.println("Gender:" + gender); //male
        sex = false;
        gender = sex ? 'male' : 'female';
        System.out.println("Gender:" + gender); //female
        //Another example
        int x = 100;
        int y = 100;
        System.out.println(x==y?"x and y equal":"x and y Unequal");
    }
}

7, String concatenation operator

Let's look at a program:

class PlusTest {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        //Both sides of the plus sign are int types to perform summation
        int c = a + b;
        /*
         * Although the following code has two plus signs, the first plus sign is in double quotation marks,
         * It doesn't work. It's just an ordinary string. The second plus sign is due to
         * The left operand is a string, so this plus sign must be used
         * String connection. The result after connection is still a string.
         */
        System.out.println("a + b = " + c); //a + b = 30
        /*
         * Analyze the output of the following programs? Which plus signs are summed,
         * Which plus signs connect strings
         */
        System.out.println(a + " + " + b + " = " + a + b);
        /*
         * Analyze the output of the following programs? Which plus signs are summed,
         * Which plus signs connect strings
         */
        System.out.println(a + " + " + b + " = " + (a + b));
        //Define a string type variable
        String name = "jack";
        /*
         * String splicing is very common in practical development. There are pithy formulas when splicing,
         * Remember, you can splice variables into strings like this
         * Do: 1. Add an English double quotation mark at the splicing position; 2. In double citation
         * Add two plus signs in the middle of the sign; 3. Put the string variable into two plus signs
         * middle.
         */
        System.out.println("Login succeeded, welcome" + name + "Come back!");
    }
}

Tags: Java

Posted on Sat, 20 Nov 2021 22:16:36 -0500 by kasitzboym