基本运算符

基本运算符


算数运算符

+-*/%++
自增自减

除法时,要把类型强转为double

package operator;

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

        System.out.println(a+b);     //30
        System.out.println(a-b);     //-10
        System.out.println(a*b);     //200
        //除法要将类型强转为double类型,因为int类型没有小数
        System.out.println(a/(double)b);    //0.5

    }

}

不同类型相加时,自动转换为最高级数据类型,如果都是最小类型,那么都变成int

package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123L;
        int b = 123;
        short c = 10;
        byte d = 1;
        double e = 11.5;

        //数中有Long型,所有数都变成Long型,如果都是最小类型,那么都变成int型
        System.out.println(a+b+c+d);  //123123257
        System.out.println(b+c+d);    //134
        System.out.println(c+d);      //11
        System.out.println(a+e);      //1.231231345E8
    }
}
package operator;

public class Demo04 {
    public static void main(String[] args) {
        int a = 10;
        int b = a++;  //先输出,再加
        int c = ++a;  //先加,在输出

        System.out.println(a);  //12
        System.out.println(b);  //10
        System.out.println(c);  //12
    }
}

比较运算符

><==>=<=!=
大于小于等于大于等于小于等于不等于
package operator;

public class Demo03 {

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

        System.out.println(a>b); //false
        System.out.println(a<b); //true
        System.out.println(a==b);//false
        System.out.println(a!=b);//true

        System.out.println(c%a); //1
    }
}

逻辑运算符

&&||!
package operator;

public class Demo05 {
    public static void main(String[] args) {

        boolean a = true;
        boolean b = false;

        System.out.println("a && b =" + (a&&b));      //逻辑与  false
        System.out.println("a || b =" + (a||b));      //逻辑或  true
        System.out.println("!(a && b) =" + !(a&&b));  //逻辑非  true

        //短路运算

        int c = 5;
        boolean d = (c<4)&&(c++>4);

        System.out.println(c);  //5
        System.out.println(d);  //false
    }
}

短路运算说明 运算如果第一个条件就有答案,那么将不会继续验证第二个条件

位运算符

&|^~
异或
package operator;

public class Demo06 {
    public static void main(String[] args) {
        /*
        位运算符
        A 0100 1101
        B 1011 0110
        
        A & B 0000 0100 两个都为1才为1
        A | B 1111 1111 有一个为1就为1
        A ^ B 1111 1011 相同的为0,不同的为1
        ~B 0100 1001    与原来的不同
         */
    }
}

移位运算符

<<>>>>>
左移带符号右移无符号右移
package operator;

public class Demo07 {
    public static void main(String[] args) {
        System.out.println(2<<3);  //16
        System.out.println(16>>1); //8
    }
}

将十进制转换成二进制,左移几位就是把高位去掉几位,低位用零补足,负数用1补足

可以理解为左移几位,就是*2的几次方

右移几位,就是/2的几次方

扩展赋值运算符

+=-+*=/=
package operator;

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

        System.out.println(a+=b);
    }
}

条件运算符

? :

package operator;

public class Demo09 {
    public static void main(String[] args) {
        int score = 80;
        String type = score>60 ? "及格" : "不及格";
        System.out.println(type);
    }
}

狂神说视频讲解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值