Daily learning java series – Java zero basic learning notes (VII) Java operator

Time:2021-8-14

operator

Arithmetic operators:

+ – * / % ++ —

Key points:++

++No matter before or after the variable, as long as the + + operation ends, it will automatically add 1

int i = 10;
        i++;
        System.out.println(i); // 11
                        int k = 10;
        ++k;
        System.out.println(k); // 11

        ++Appear before variable:
            int i = 10;
            int k = ++i;
            System.out.println(k); // 11
            System.out.println(i); // 11
        
        ++Appears after variable:
            int i = 10;
            int k = i++;
            System.out.println(k); // 10
            System.out.println(i); // 11
        
        int i = 10;
        System.out.println(i++); // 10
        What is the key to solving the above problems? Disassembly code
            int temp = i++;
            System.out.println(temp); // 10
            System.out.println(i); // 11
        
        int i = 10;
        System.out.println(++i);
        Disassembly Code:
            int temp = ++i;
            System.out.println(temp); // 11
            System.out.println(i); // 11

Relational operators:

> >= < <= == !=

The results are Boolean. true/false

Logical operators:

& | ! && ||

Logical operators require Boolean types on both sides and the final result is Boolean.

Boolean type on the left & boolean type on the right — > the final result is boolean type.

&Both sides are true, and the result is true

|One side is true, and the result is true

! Reverse

&&In fact, the result is exactly the same as that of & except that & & has short circuit.

When the left is false: & & short circuit.

When the left is true: | short circuit.

Assignment operator:

= += -= *= /= %=

Important rules:

When using the extended assignment operator, it should be noted that no matter how the operation is performed, the final operation result type will not change.

byte x = 100; //  Byte maximum 127

x += 1000; //  The compilation can pass. The X variable is still of byte type, but the accuracy is lost.

x += 1000;  Equivalent to: x = (byte) (x + 1000);
                
int i = 10;

i += 10; //  Equivalent to: I = I + 10; Add up.

Ternary operator:

Boolean expression? Expression 1: expression 2

If the Boolean expression is true, select expression 1 as the result.

Conversely, select expression 2 as the result.

String concatenation operator:

+

+Numbers on both sides, sum.

+There is a string on one side for string splicing.

+If there are multiple, follow the sequence from left to right: 1 + 2 + 3

If you want one of the plus signs to execute first, you can add parentheses: 1 + (2 + 3)

Note: the result after string splicing is still a string.

Tip: how to insert a variable into a string.

String name = "jackson";
System.out.println ("login succeeded, welcome" + name + "back");

How to receive user keyboard input?

java.util.Scanner s = new java.util.Scanner(System.in);

//Receive integer

int i = s.nextInt()

//Receive string

String str = s.next();

last

I recommend you a more detailed Java zero foundation tutorial. The following one I have seen is very good and worth watching and collecting.

To share with you, click here

https://www.bilibili.com/vide…

After reading, if it helps you, thank you for your praise and support~