当前位置:网站首页>Basic operator

Basic operator

2022-06-25 23:23:00 Seasonal white September

List of articles

Operator

Arithmetic operator :+,—,*,/,%,++,–

Assignment operator :=

Relational operator :>,<,<=,>=,==,!=instanceof

Logical operators :&&,||,!

An operator :&,|,^,~,>>,<<,>>>

Conditional operator : ?:

Extended assignment operators :+=,-=,*=,/=

package operator;

public class Demo01 {
    
    public static void main(String[] args) {
    
        // Binary operator 
        int a=10;
        int b=20;
        int c=25;
        int d=25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
        
        
    }
}
package operator;

public class Demo2 {
    
    public static void main(String[] args) {
    
        long a=1564315151313L;
        int b=123;
        short c=10;
        byte d=8;
        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        
        // If there is   Advanced data types   Then convert to advanced type   In the operation  ; As in the formula   Yes long It is long type ;
        //  Or there is double It is doule type 
        // otherwise   Convert to the lowest  int  type 

    }
}
package operator;

public class Demo03 {
    
    public static void main(String[] args) {
    
        // Relational operators return results : correct  , error   Boolean value 
        int a=10;
        int b=10;
        int c=21;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        // Remainder or Modular arithmetic 
        System.out.println(c%a);// c/a 21/10= 2......1
    }
}
package operator;

public class Demo04 {
    
    public static void main(String[] args) {
    
        // ++ --  Self increasing  , Self reduction   Unary operator 
        int a=3;    //a=3
        int b=a++; //b=a=3 a=a+1 a=4
        int c=++a; //c=a+1=5 a=a+1=5
        System.out.println(a);//a=5
        System.out.println(b);//b=3
        System.out.println(c);//c=5

        // Power operation  2^3 2*2*2 =8  A lot of operations , We will use some tool classes to operate  Math class 
        double pow =Math.pow(2,3);
        System.out.println(pow);
    }
}
package operator;

public class Demo05 {
    
    public static void main(String[] args) {
    
        //  And  (and)  or (or)  Not  ( Take the opposite )
        boolean a=true;
        boolean b=false;
        System.out.println("a&&b:"+(b&&a));// Logic and computation  : Both variables are true   The result is true
        System.out.println("a||b:"+(a||b));// Logic or operation  : One of the two variables is true   The result is true
        System.out.println("!(a&&b):"+!(a&&b));// Logical non operation  : Both variables are false   The result is true

        // Short-circuit operation  b&&a when  b It's already fake   了  , There's no need to   Continue to calculate the following 
        int c=5;
        boolean d=(c<4)&&(c++<4);
        System.out.println(d);//false
        System.out.println(c);//5
    }
}
package operator;

public class Demo06 {
    
    public static void main(String[] args) {
    
        /* A=0011 1100 B=0000 1101 A&B:0000 1100  The corresponding bits are 1 Then for 1; Otherwise 0 A|B:0011 1101  The corresponding bits are 0 Then for 0; Otherwise 1 A^B:0011 0001  If the corresponding bits are the same, it is 0; Otherwise 1 ~B:1111 0010  Take the opposite  */
        //2*8=16 2*2*2*2  How to calculate the fastest   An operation 
        //<< >>  Move left   Move right 
        System.out.println(2<<3); 
        /* 0000 0000 0 0000 0001 1 0000 0010 2 0000 0011 3 . . . 0000 1000 8 0001 0000 16  Move left times 2  Move right divided by two   Very efficient  */
    }
}

Output connector use plus Express

package operator;

public class Demo07 {
    public static void main(String[] args) {
        int a=10;
        int b=20;

        a+=b;// a=a+b
        a-=b;// a=a-b

        // String connector   + ,String    The string appears before   Take the formula as   character string   Connect 
        // if   The string appears after   Then the previous formula goes first   operation   No effect  
        System.out.println(""+a+b);
        System.out.println(a+b+"");

    }
}

Output results as follows :

1020
30

package operator;

public class Demo08 {
    
    public static void main(String[] args) {
    
        // x?y:z
        //if x==true  The result is  y , Otherwise, the result is  z

        int score=80;
        String type =score<60?" fail, ":" pass ";
        System.out.println(type);
    }
}

priority

Brackets Internal priority Use more parentheses

原网站

版权声明
本文为[Seasonal white September]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180604173563.html