运算符

--算术运算符:+ - * / % ++ --
--赋值运算符:=
--关系运算符:> < >= <= == != instanceof
--逻辑运算符: && || !
--位运算符:&(按位与) |(按位或) ^(按位异或) ~(按位取反) >>(右移 /2取商) <<(左移*2)
--条件运算符:x?y :z
--扩展赋值运算符: += -= *= /=

public class TestOperator{
    public static void main(String[] args){
        /*
        //算术运算符
        int a=3;
        int b=a++;   //a++先赋值,再+1
        int c=++a;   //++a,先+1,再赋值
        System.out.println(a); //5
        System.out.println(b); //3
        System.out.println(c); //5
        */
        
        /*
        //逻辑运算符
        boolean c=1>2&&2>(3/0);
        System.out.println(c); //false
        
        boolean d=1<2&&2>(3/0);
        System.out.println(d); //ArithmeticException
        */
        
        //位运算符
        int m=8;
        int n=4;
        System.out.println(m&n);//0
        System.out.println(m|n);//12
        
        int a=3*2*2;
        int b=3<<2;
        int c=12/2/2/2;
        int d=12>>3;
        System.out.println(a);//12
        System.out.println(b);//12
        System.out.println(c);//1
        System.out.println(d);//1
        
        System.out.println(4+"5");  //45 +前后有字符串,字符串拼接
        
        //条件运算符
        int x=2;
        int y=3;
        String z=(x<y)?"x<y":"x>=y";
        System.out.println(z);    
        
    }
}

 

posted on 2020-02-20 20:38  happygril3  阅读(214)  评论(0)    收藏  举报

导航