[JAVA knowledge point daily practice] – those things about operators

Hits: 0

It is said that the foundation is not firmly shaken , but this is really the case! Don’t underestimate this [operator] , the operator plays a pivotal role in each grammar. If it’s useless, it’s a big mistake!

Next, follow Xiaomeng and take a relaxed and happy pace to see those things about operators~

operator thing

🌊Operators supported by the Java language

💧 Arithmetic Operators

💧 Assignment operator

💧 Extended assignment operator

💧 Relational Operators

💧 Logical Operators

💧 Conditional Operators

🌊 Operator precedence

🌊Operators supported by the Java language

  • arithmetic operators

public class TestOpe01 {
public static void main ( String[] args ) {
//Print result:
System. out .println( 12 / 3 );
System.out.println(12%5);
System.out.println(12/3.0);
System.out.println(12%5.0);
}
}

  • assignment operator 

public class Test03 { public static void main ( String[] args ) { //Indicates positive numbers: System.out .println (+ 5 ); //5 //Addition operation : System.out .println ( 5 + 6 ) ; //11 System.out .println ( 5 + '6' ); //59 //Concatenation of strings: //Rule: If there is a string on either side of +, then the plus sign is a string The effect of splicing, the result must be the string int num = 56 ;</p> <pre><code> System.out.println("num="+num);//"num=56" ---> num=56 System.out.println(5+6+"7");//11+"7"--->"117" --->117 System.out.println(5+'6'+"7");//59 +"7"--->"597" --->597 System.out.println("5"+6+"7");//"56"+"7" --->"567"--->567 System.out.println("5"+'6'+"7");//"56"+"7"--->"567"--->567 System.out.println("5"+'6'+'7');//"56"+'7'--->"567"---567 } </code></pre> <p>}

  • spread assignment operator

int a = 5; a++; //understood as: equivalent to a=a+1 operation</p> <p>a = 5; ++a; //Understand as: equivalent to a=a+1 operation</p> <p>a = 5 ; int m = a++ + 7 ; // do m=a+7 first and add 1: a = a+1</p> <p>a = 5 ; int n = ++a + 7 ; //add 1 first a=a+1 and then operate: n = a+7

  • relational operator

int a = 30;
int b = 50;

  • Logical Operators

//>, <, >=, <=, ==, !=
// Final result of relational operators: either true or false
System. out .println( 5 > 11 ); //false
System. out .println ( 5 < 11 ); //true System.out.println
( 5 > = 11 ); // false
System.out.println ( 5 <= 11 ); // true
System.out.println ( 5 == 11 ) ; //false System.out.println
( 5 ! = 11);//true
System.out.println((5<11)!=(9==12));//true

  • conditional operator

// Logical AND &: As long as one of the operands is false, the result must be false System.out .println ( true & true ); System.out.println(true&false); System.out.println(false&false); System.out.println(false&true);</p> <p>// Short circuit and &&: more efficient, as long as the first expression is false, then the second expression does not need to be calculated, the result must be false System. out .println( true && true ); System.out.println(true&&false); System.out.println(false&&false); System.out.println(false&&true);</p> <p>// Logical OR |: As long as one operand is true, the result must be true System. out .println( true | true ); System.out.println(true|false); System.out.println(false|false); System.out.println(false|true);</p> <p>// Short circuit or ||: It is more efficient, as long as the first expression is true, then the second expression does not need to be calculated, and the result must be true System. out .println( true || true ); System.out.println(true||false); System.out.println(false||false); System.out.println(false||true);</p> <p>//Logical NOT!: The opposite result System.out.println (! true ); //false System.out.println ( ! false ) ; //true</p> <p>//Logical XOR ^: The two operands are the same, the result is false, not the same, the result is true System.out .println ( true ^ true ); System.out.println(true^false); System.out.println(false^false); System.out.println(false^true);

  • bitwise operators

?:

I saw earlier that JAVA supports so many operators, and the seconds used by operators can simplify a lot of code! Review carefully~

💧 Arithmetic Operators

/ Division operator : Indicates the division of two numbers

% remainder operator: used to find the remainder

public  class  TestOpe01 {
         public  static  void  main ( String[] args ) {
                 //Print result: 
                System. out .println( 12 / 3 );
                System.out.println(12%5);
                System.out.println(12/3.0);
                System.out.println(12%5.0);
        }
}

+ : Represents a positive number, represents an addition operation, and concatenates strings

public  class  Test03 {
         public  static  void  main ( String[] args ) {
                 //Indicates positive numbers: System.out 
                .println (+ 5 ); //5 //Addition operation 
                : System.out .println ( 5 + 6 ) ; //11 System.out 
                .println ( 5 + '6' ); //59 //Concatenation of strings: //Rule: If there is a string on either side of +, then the plus sign is a string The effect of splicing, the result must be the string int num = 56 ;




                System.out.println("num="+num);//"num=56" ---> num=56
                System.out.println(5+6+"7");//11+"7"--->"117"  --->117
                System.out.println(5+'6'+"7");//59 +"7"--->"597" --->597
                System.out.println("5"+6+"7");//"56"+"7"  --->"567"--->567
                System.out.println("5"+'6'+"7");//"56"+"7"--->"567"--->567
                System.out.println("5"+'6'+'7');//"56"+'7'--->"567"---567
        }
}

++: When ++ is used alone, whether it is placed before or after, it is an operation of adding 1. When participating in the operation, see whether ++ is before or after, if ++ is after: operate first, then add 1. If ++ is before, add 1 first, then do the operation.

int a = 5;
a++; //understood as: equivalent to a=a+1 operation

a = 5;
++a; //Understand as: equivalent to a=a+1 operation

a = 5 ;
 int m = a++ + 7 ; // do m=a+7 first and add 1: a = a+1

a = 5 ;
 int n = ++a + 7 ; //add 1 first a=a+1 and then operate: n = a+7

💧 [Assignment operator]

=: assigns the value on the right side of the equal sign to the left side of the equal sign

int a = 30;
int b = 50;

💧 Extended assignment operator

a+=b Sum a=a+b Division:

a+=b    is slightly less readable and has high compilation efficiency. The bottom layer automatically performs type conversion

a=a+b   has good readability, low compilation efficiency, manual type conversion

I didn’t expect that there are such differences between small operators, don’t panic, there are still more, let’s see~        

💧 [Relational Operators]

I believe that whether you are studying or working, the frequency of use of relational operators is a lever! Xiaomeng directly goes to the code to take everyone to review it. If you are not clear, please practice more~

//>, <, >=, <=, ==, != 
// Final result of relational operators: either true or false 
System. out .println( 5 > 11 ); //false 
System. out .println ( 5 < 11 ); //true System.out.println 
( 5 > = 11 ); // false 
System.out.println ( 5 <= 11 ); // true 
System.out.println ( 5 == 11 ) ; //false System.out.println 
( 5 ! = 11);//true
System.out.println((5<11)!=(9==12));//true

Notice! Relational operator final result: either true or false 

💧 [Logical Operators]

Logical operator: For logical operations, the left and right connections of the operator are all Boolean operands, and the result of the final expression is a Boolean value. Either true or false.

Logical AND &: As long as one of the operands is false, the result must be false

Short circuit and &&: more efficient, as long as the first expression is false, then the second expression does not need to be calculated, and the result must be false

Logical OR|: As long as one of the operands is true, the result must be true

Short circuit or ||: More efficient, as long as the first expression is true, then the second expression does not need to be calculated, and the result must be true

Logical NOT ! : the opposite result

Logical XOR^: The two operands are the same, the result is false, not the same, the result is true

// Logical AND &: As long as one of the operands is false, the result must be false System.out 
.println ( true & true );
System.out.println(true&false);
System.out.println(false&false);
System.out.println(false&true);

// Short circuit and &&: more efficient, as long as the first expression is false, then the second expression does not need to be calculated, the result must be false 
System. out .println( true && true );
System.out.println(true&&false);
System.out.println(false&&false);
System.out.println(false&&true);

// Logical OR |: As long as one operand is true, the result must be true 
System. out .println( true | true );
System.out.println(true|false);
System.out.println(false|false);
System.out.println(false|true);

// Short circuit or ||: It is more efficient, as long as the first expression is true, then the second expression does not need to be calculated, and the result must be true 
System. out .println( true || true );
System.out.println(true||false);
System.out.println(false||false);
System.out.println(false||true);

//Logical NOT!: The opposite result 
System.out.println (! true ); //false System.out.println ( ! 
false ) ; //true

//Logical XOR ^: The two operands are the same, the result is false, not the same, the result is true System.out 
.println ( true ^ true );
System.out.println(true^false);
System.out.println(false^false);
System.out.println(false^true);

💧 [Conditional Operators]

Ternary operator Format : a?b:c

Where a is a Boolean expression, the return result is either true or false, and the result of the final expression is determined by the result of a

  • If the result of a is true, then the final result of the expression is b
  • If the result of a is false, then the final result of the expression is c

🌊 Operator precedence

Here, Xiaomeng reminds everyone that the priority of area-level operators should not be used. We must gradually master it in practical application, and it will come naturally. In fact, in practice, we will not write some very complex expressions, how can we write them easily.

Priority relation: assignment < ternary < logical < relation < arithmetic < unary

If the article is helpful to your friends, please give Xiaomeng a 👍 one~ Thank you to my family~

You may also like...

Leave a Reply

Your email address will not be published.