【JAVA】运算符表达式

算术运算

  +  -  *  /  %(取余)  ++ (自增)  -- (自减)

public class MathCalcu{

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

       int jiafa = a + b;
       System.out.println("a + b = " + jiafa);

       int jianfa = a - b;
       System.out.println("a - b = " + jianfa);

       int chengfa = a * b;
       System.out.println("a * b = " + chengfa);

       int chufa = a / b;
       System.out.println("a / b = " + chufa);

       int quyu = a % b;
       System.out.println("a % b = " + quyu);
       
    }
}

 

编译结果:  

 

  i++   // 先赋值后加1
  ++i   // 先加1后赋值

public class MathCalcu{

    public static void main(String[] args){
        int a = 10;
        int b = 2;
        // 后缀自增自减法(a++,a--): 先进行表达式运算,再进行自增或者自减运算
        System.out.println("a = " + (a));
        System.out.println("a++ = " + (a++));
        System.out.println("a = " + (a));
        System.out.println("a-- = " + (a--));
        System.out.println("a = " + (a));
        System.out.println("=======================");
        // 前缀自增自减法(++b,--b): 先进行自增或者自减运算,再进行表达式运算
        System.out.println("b = " + (b));
        System.out.println("++b = " + (++b));
        System.out.println("--b = " + (--b));
        System.out.println("b = " + (b));

    }
}

 

 

编译结果:  

 

 

 

 

 

 

关系运算

  >  <  >=  <=  ==  !=    【得到的结果是一个布尔类型】

public class MathCalcu{

    public static void main(String[] paramArrayOfString) {
        byte b1 = 3;
        byte b2 = 4;
        boolean bool = (b1 > b2) ? true : false;
        System.out.println("result = " + bool);
        bool = (b1 <= b2) ? true : false;
        System.out.println("result = " + bool);

        System.out.println("3 == 4   -->  false");
        System.out.println("3 != 4   -->  true");
    }
}

 

编译结果:  

 

位运算: 二进制运算 0 1

A = 0011 1100  = 60
B = 0000 1101  = 13

 

 

public class MathCalcu{

    public static void main(String[] paramArrayOfString) {
        byte b1 = 4;  //0000 0100
        System.out.println(b1 ^ 0xFFFFFFFF);    // 异或
        byte b2 = 6;  //0000 0110
        System.out.println(b1 & b2);
        System.out.println(b1 | b2); 
        System.out.println(b1 ^ b2); 
        System.out.println(b2 << 2);
        System.out.println(b2 >>> 2);
    }
}

 

 

编译结果:  

 

 

 

 

逻辑运算


  && : 逻辑与(短路与)
    true && true = true;
    true && false = false;
    false && true = false;
    false && false = false;
  || : 逻辑或
    true || true = true;
    true || false = true;
    false || true = true;
    false || false = false;
  ! : 逻辑非
    !true = false;
    !false = true;

public class MathCalcu{

    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        System.out.println("a && b = " + (a&&b));
        System.out.println("a || b = " + (a||b) );
        System.out.println("!(a && b) = " + !(a && b));
    }
}

 

编译结果:  

 

 

 

赋值运算


 

 

 

 

 

条件运算(三目运算)

  布尔类型表达式?值1:值2;
    为真,结果为值1;
    为假,结果为值2;

public class MathCalcu{

    public static void main(String[] args) {
        int a = 2;
        int b = 3;
        int c = a > b ? a : b;
        boolean d = a > b ;
        System.out.println("表达式结果为:" + d);
        System.out.println("c = " + c);
    }
}

 

编译结果:  

 

 

 

 

 运算符优先级排序

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2022-05-09 16:47  Phoenixy  阅读(37)  评论(0编辑  收藏  举报