Java04-运算符

运算符

Java语言支持的运算符

  1. 算术运算符:+ , - , * , / , % , ++ , --
  2. 赋值运算符:=
  3. 关系运算符:< , > , >= , <= , == ,!=instanceof
  4. 逻辑运算符:&&,||,!
  5. 位运算符:&,|,^,~,>>,<<,>>>(了解!!!)
  6. 条件运算符:?:
  7. 扩展赋值运算符:+=,-=,*=,/=

算数运算符示例

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl+D : 复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);//->30
        System.out.println(a-b);//->-10
        System.out.println(a*b);//->200
        System.out.println(a/(double)b);//->0.5 结果为小数时,要注意作用范围
        System.out.println(a%b);//->10
    }
}

算数运算符计算过程中数据类型的变化

public class Demo02 {
    public static void main(String[] args) {
        long a = 1232414535L;
        int b = 123;
        short c = 10;
        byte d = 8;
        float e = 9;
        double f = 11;
        //计算过程中,有Long类型,输出值会变为Long,有float变float,有double会变double,否者为int类型
        System.out.println(a+b+c+d);//->1232414676  Long
        System.out.println(b+c+d);//->141  int
        System.out.println(c+d);//->18  int
        System.out.println(a+e);//->1.23241459E9  float
        System.out.println(a+f);//->1.232414546E9  double
        System.out.println(f+e);//->20.0  double
        System.out.println(b%c);//->3  取余,模运算
    }
}

关系运算符示例

public class Demo3 {
    public static void main(String[] args) {
        //关系运算符的返回结果:true  false
        int a = 10;
        int b = 20;
        System.out.println(a>b);//->false
        System.out.println(a<b);//->true
        System.out.println(a==b);//->false
        System.out.println(a!=b);//->true
    }
}

自增运算符(自减同理)&& 幂运算

public class Demo04 {
    public static void main(String[] args) {
        //++   --   自增   自减     一元运算符
        int a = 3;
        int b = a++;//在执行这行代码后,先给b赋值,再自增
        //a = a + 1
        System.out.println(a);//->4
        //a = a + 1
        int c = ++a;//a = a + 1  在执行这行代码前,先自增,再给b赋值

        System.out.println(a);//->5
        System.out.println(b);//->3
        System.out.println(c);//->5

        //幂运算  2^3  2*2*2    很多运算,我们会使用一些工具类来操作!!!
        double pow = Math.pow(2,3);
        System.out.println(pow);//->8.0
    }
}

逻辑运算符&&短路运算

public class Demo05 {
    public static void main(String[] args) {
        //与(and)  或(or)  非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:" + (a && b));//->a && b:false
        System.out.println("a || b:" + (a || b));//->a || b:true
        System.out.println("!(a && b):" +(!(a && b)));//->!(a && b):true

        //短路运算
        int c = 5;
        boolean d = (c<4) && (c++<4);
        System.out.println(d);//->false
        System.out.println(c);//->5  说明在与运算中,在第一项为0后,第二项c++没有进行运算
    }
}

位运算示例

public class Demo06 {
    public static void main(String[] args) {
        /*
        A  =  0011 1100
        B  =  0000 1101
        --------------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001 异或
        ~B  = 1111 0010

        2*8 = 16 2*2*2*2
        效率极高!!!!
        <<  *2
        >>  /2

        0000 0000   0
        0000 0001   1
        0000 0010   2
        0000 0100   4
        0000 1000   8
        0001 0000   16
         */
        System.out.println(2<<3);
    }
}

字符串连接符”+“

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 :"+"前有一项是String类型,后面的内容都会变成字符串类型
        System.out.println(a+b);//->30
        System.out.println(""+a+b);//->1020
        System.out.println(a+b+"");//->30
    }
}

三元运算符

//三元运算符
public class Demo08 {
    public static void main(String[] args) {
        //x ? y : z
        //如果x==true,则结果为y,否则结果为z
        int score = 80;
        String type = score > 60 ? "pass":"not pass";//必须掌握
        System.out.println(type);
    }
}

优先级

posted @ 2022-08-18 15:48  J-Hoko  阅读(15)  评论(0)    收藏  举报