“This is the 15th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

I believe many friends have encountered a problem, the priority of the operator, in the process of running the program which operations are carried out first, which operations are executed after. Many beginners suffer from this problem. This time, we will introduce a simple system based on operators.

The operator

The process of running the program is the process of continuous mathematical operations, there are also a variety of operators in Java, mainly including operators: arithmetic operators, relational operators, bitwise operators, logical operators, assignment operators, other operators. Arithmetic operators are used in mathematical expressions in the same way they are used in mathematics.

Arithmetic operator

The operator The name of the describe
+ add Add two values
Reduction of The two values are subtracted
* take Multiply two values
/ In addition to Two values divide
% Take more than remainder
++ Since the increase The value of the operand increases by 1
Since the reduction of The value of the operand decreases by 1

The following is an example of an arithmetic operator:

public static void main(String[] args) {
        int i = 5;
        int j = 3;
        System.out.println("i+j = " + (i + j));
        System.out.println("i-j = " + (i - j));
        System.out.println("i*j = " + (i * j));
        System.out.println("i/j = " + (i / j));
        System.out.println("i%j = " + (i % j));
        System.out.println("i++ = " + i++);
        System.out.println("j-- = " + j--);
        System.out.println("i = " + i);
        System.out.println("j = " + j);
        System.out.println("++i = " + ++i);
        System.out.println("--j = " + --j);

    }
Copy the code

Execute the above arithmetic operator to print the result:

i+j = 8
i-j = 2
i*j = 15
i/j = 1
i%j = 2
i++ = 5
j-- = 3
i = 6
j = 2
++i = 7
--j = 1
Copy the code

As you can see from the output, i++ and j– are used first and then incremented or decayed. Plus plus I and –J are either incremented or subtracted by one.

Relational operator

The operator The name of the describe
= = equal The left and right sides of the operator are true
! = Is not equal to The operator is true if the left and right values are different
> Is greater than Operator is true if the value on the left is greater than the value on the right
< Less than The operator is true if the left value is less than the right value
> = Greater than or equal to Operator if the left-hand value is greater than or equal to the right-hand value is true
< = Less than or equal to The left-hand value is less than or equal to the right-hand value is true
The sample

The following is an example of a relational operator:

public static void main(String[] args) { int i = 5; int j = 3; System.out.println(" I ==j = "+ (I ==j)); System.out.println("i! =j = "+ (I! =j)); System.out.println(" I >j = "+ (I >j)); System.out.println(" I <j = "+ (I <j)); System.out.println(" I >=j = "+ (I >=j)); System.out.println(" I <=j = "+ (I <=j)); }Copy the code

After executing the above relational operator, the output is:

I = = j to false I! =j is true I >j is true I <j is false I >=j is true I <=j is falseCopy the code

An operator

Bitwise operation is the one – and two-bit operation of bitwise mode or binary number in programming.

The operator The name of the describe
& with 1 for both 1 and 0 for none
| or One is one and the other is zero
^ non Equal to 0, otherwise equal to 1
~ Bitwise inverse operator The bitwise inverse operator flips each bit of the operand, that is, 0 becomes 1, and 1 becomes 0.
<< Bitwise left shift operator The left operand moves the right operand by bits to the left
>> Bitwise right shift operator The left operand moves right by bit to the number of digits specified by the right operand
>>> Bitwise right shift zeroing operator The resulting empty space is filled with zero

Logical operator

The operator The name of the describe
&& Logic and If and only if both operands are true
| | Logic or If either of the two operands is true w
! Logic is not Used to reverse the logical state of operands. If the condition is true, the logical non-operator will get false.

The following is an example of a logical operator:

public static void main(String[] args) {
        int i = 5;
        int j = 3;
        System.out.println("i && j 为 " + (i==5 && j==3));
        System.out.println("I | | j as" + (i==5 || j==3));
        System.out.println("i ! j 为 "+ (i ! =j)); }Copy the code

The logical operator runs as follows:

I & jtrueI | | jtruei! J fortrue
Copy the code

The assignment operator

The operator The name of the describe
= The assignment operation
+ = Add and assignment
– = Reduction and assignment
* = Multiplication and assignment
/ = In addition to and assignment
(%) = Modulo and assignment
< < = Left shift assignment
> > = Right shift assignment
& = Bitwise and
^ = The bitwise exclusive or
| = Bitwise or

An example of the assignment operator is as follows:

 public static void main(String[] args) {
        int i = 5;
        int j = 3;
        System.out.println("I = j as" + (i =j));
        i = 5; j =3;
        System.out.println("I + = j as" + (i +=j));
        i = 5; j =3;
        System.out.println("I - = j as" + (i -=j));
        i = 5; j =3;
        System.out.println("I * = j as" + (i *=j));
        i = 5; j =3;
        System.out.println("I / = j as" + (i /=j));
        i = 5; j =3;
        System.out.println("I (%) =j =" + (i %=j));
        i = 5; j =3;
        System.out.println("I < < = j as" + (i <<=j));
        i = 5; j =3;
        System.out.println("I > > = j as" + (i >>=j));
        i = 5; j =3;
        System.out.println("I & = j as" + (i &=j));
        i = 5; j =3;
        System.out.println("I ^ = j as" + (i ^=j));
        i = 5; j =3;
        System.out.println("I | = j as" + (i |=j));

    }
Copy the code

The assignment operator runs as follows:

I = j3I + = j8I - = j2I * = j15I / = j1I (%) =j is2I < < = j40I > > = j0I & = j1I ^ = j6I | = j7

Copy the code

Ternary operator

The ternary operator has three operands and needs to determine the value of a Boolean expression. The preceding is the expression, which prints the value of the second operand if the expression’s value is true, and the third operand if the result of the trinary operator is false

The operator The name of the describe
? : Is the expression true or false? True value: false value

The following is an example of a ternary operator:

public static void main(String[] args) {
        int i = 5;
        int j = 3;
        String str = i > j ? "i>j" : "i<j";
        System.out.println(str);
    }
Copy the code

The ternary operator runs as follows:

i>j
Copy the code

Java operator priority

When multiple operators occur in an expression, we need to follow a certain order of execution, otherwise various results may occur. At the end of this article, we summarize the basic execution order of the operators, where the lower left is preferred. The priorities from highest to lowest are:

Unary operations > multiplication and division > addition and subtraction > Shift > Relation > Equality > bitwise and > bitwise xor > bitwise or > Logical and > Logical or > Ternary operators > assignment

conclusion

Well, the above is a summary of common Java operators and their priorities, thank you for reading, I hope you like, if you are helpful, welcome to like favorites. If there are shortcomings, welcome comments and corrections. See you next time.

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.