java基本运算符

java基本运算符

  • 算术运算符:+,-,*,/,%,++,–
  • 赋值运算符:=
  • 关系运算符:>,<,>=,<=,==,!=,instanceof
  • 逻辑运算符:&&,||,!
  • 位运算符:&,|,^,~,>>,<<,>>>
  • 条件运算符:?:
  • 扩展赋值运算符:+=,-=,*=,/=
public class demo22 {
    public static void main(String[] args) {
        //二元运算符
        int a=1;
        int b=2;
        int c=3;
        int d=4;
        System.out.println(a+b);  //加
        System.out.println(a-b);  //减
        System.out.println(a*b);  //乘
        System.out.println(a/(double)b);  //除
        System.out.println(a%b);   //取余
        System.out.println("------------");

        long a1=12344565364346l;
        int b1=100;
        byte c1=10;
        short d1=4;
        System.out.println(a1+b1+c1+d1);  //结果还是long类型
        System.out.println(b1+c1+d1);  //结果为int类型
        System.out.println(c1+d1);   //结果为int类型
        
    }

}

在这里插入图片描述

public class demo22 {
    public static void main(String[] args) {
       //++ 自增   --自减
        int a=1;
        int b=++a;  //++a 是先执行加1,再将加了1后的a赋给b
        int c=a++;  //a++ 是先将a赋给c,再执行加1

        System.out.println(b);
        System.out.println(c);
        System.out.println(a);
    }

    }

在这里插入图片描述

public class demo22 {
    public static void main(String[] args) {
        //关系运算符返回的结果为true false
        int a=1;
        int b=2;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);

    }

}

在这里插入图片描述

public class demo22 {
    public static void main(String[] args) {
        //幂运算   会使用一些工具类来操作
        double pow=Math.pow(3,2);
        double pow1=Math.pow(2,3);
        System.out.println(pow);
        System.out.println(pow1);


    }

}

在这里插入图片描述

public class demo22 {
    public static void main(String[] args) {
        //逻辑运算符  与  或  非
        Boolean a=true;
        Boolean b=false;
        System.out.println("a&&b:"+(a&&b));  //逻辑与运算:两个变量都为真,结果为true
        System.out.println("a||b:"+(a||b));  //逻辑或运算:两个变量有一个为真,结果为true
        System.out.println("!(a&&b):"+!(a&&b));  //逻辑非运算:如果是真,则变为假,反之亦然

        //短路运算
        int c=6;
        int c1=8;
        boolean d=(c<4)&&(c++>5);
        boolean e=(c1>4)&&(c1++>5);

        System.out.println(d);
        System.out.println(c);  //c的值没有变化,说明与运算时,与符号前边的如果为假,就不会执行与符号后边的运算
        System.out.println(e);
        System.out.println(c1);

    }

}

在这里插入图片描述

public class Demo03 {
    public static void main(String[] args) {
        /*
        A=0011 1100
        B=0000 1101
        A&B=0000 1100  //位与:均为1时结果为1
        A|B=0011 1101    //位或:只要有1结果为1
        A^B=0011 0001     //位异或:只要有一个1时结果为1
        ~B=1111 0010     //位反


       问题:如何计算2*8最快
       2*8=2*2*2*2
       用位运算,效率贼高
       <<    等于*2
       >>    等于/2
       0000 0000     0
       0000 0001     2
       0000 0010     4
       0000 0100     8
       0000 1000     16
         */
        System.out.println(2<<3);
    }
}
public class Demo033 {
    //三目运算符
    public static void main(String[] args) {
       //  x? y:z   如果x为true,结果为y,否则结果为z
        int years=2022;
        String tyo=years>2020?"正确":"错误";
        String tyo1=years<2020?"正确":"错误";
        System.out.println(tyo);
        System.out.println(tyo1);
    }
}
public class Demo033 {

    public static void main(String[] args) {
       int a=5;
       int b=6;
       a+=b;  //a=a+b
       a-=b;  //a=a-b
        System.out.println(a);

        //字符串连接符 +  String
        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小心!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值