Operators (assignment, comparison, logical, ternary)

/*
                                                Assignment operator
(1) += a +=3 is equivalent to a=a+3
(2) -= a -=3 is equivalent to a=a-3
(3) *= a *=3 is equivalent to a= a*3
(4)/= a /=3 is equivalent to a=a/3
(5)%= a %=3 is equivalent to a=a%3

Note: Only variables can use the assignment operator
*/
public class fx{     public static void main(String[] args){         int a=10;         a+=5;         System.out.println(a);//15     } }






 

/*
                                                Comparison operators
(1) greater than >
(2) less than <
(3) greater than or equal to >= 
(4) less than or equal to <=
(5) equal ==
(6) not equal !=

Matters needing attention: (1) The result of the comparison operator must be a boolean type, which is true, and false if it is not established
          (2) If multiple judgments are made, it cannot be written in succession, such as 1<x<3
*/
public class fx{     public static void main(String[] args){         int a=10;         int b=20;         int c=20;         System.out.println(a>b);//false         System.out.println(a<b); //true         System.out.println(a>=b);//flase         System.out.println(a<=b);//true         System.out.println(a==b);//false         System. out.println(a!=b);//ture         System.out.println(b==c);//true     } } /*                                                 Logical operator (1) and && are true (2) or || one is true  


















(3) Not ! Negative
"&&" "||" at the same time has a short-circuit effect: if the left side can already judge the final result, the right side will not be executed

 

Note: (1) Logical operators can only be used for boolen type
          (2) && || requires a boole type on the left and right, but as long as there is a unique boole type for negation
          (3) && || If multiple conditions can Write
          two conditions in a row: Condition A && Condition B
          Multiple conditions: Condition A && Condition B && Condition C
TLPS
For the case of 1<x<3, it should be split into two parts, and then connected with the AND operator
int x=2
1< x&&x<3
*/
public class fx{     public static void main(String[] args){         System.out.println(true&&false);//false         System.out.println(true&&true);//true         System.out.println( 3<5&&5<6);//true         System.out.println(true||true);//true         System.out.println(true&&false);//true         System.out.println(false&&false);//false         System.out.println(true);//true




        



        

        System.out.println(!true);//false
        
        
        //Short -circuit
        int a=10;
        System.out.println(3>4&&++a<20);//true The left side can already judge the final result, the right side will Not executing
        System.out.println(a);//10
        
        int b=20;
        System.out.println(3<4||++b<30);//true The left side can already judge the final result, the right side will Not executing
        System.out.println(b);//20
    }
}

/*
                                                Ternary operator
(1) Unary operator An operator that only needs one data to operate! ++ --
(2) Binary operator An operator that requires two data to operate + -
(3) Ternary operator An operator that requires three data to operate

 

Format: data type variable name = conditional judgment? Expression A: Expression B;
Process First, determine whether the expression is
        true. If the expression A is assigned to the variable
        , it is false. The expression B is assigned to the variable
. Note:
         (1) It must be ensured that both expression A and expression B are consistent with The data type of the left variable
         (2) The result of the ternary operation must be used
*/
public class fx{     public static void main(String[] args){         int a=10;         int b=20;         int max=a>b ?a:b;             System.out.println("The maximum value is: "+max);         System.out.println(a>b?a:b);//Correct spelling     } }







 

Guess you like

Origin blog.csdn.net/weixin_45650003/article/details/119163556
Recommended