基本运算符

12 篇文章 0 订阅
9 篇文章 0 订阅

基本运算符

算术运算符:+,-,*,/,%,++,–

赋值运算符:=

关系运算符:>,<,>=,<=,==,!=

逻辑运算符:&&(与),||(或),!(非)

位运算符:&,|,^,~,>>,<<,>>>(了解)

条件运算符:?:

扩展运算符:+=,-=,*=,/=

1、自增(++)自减(–)运算符是一种特殊的算术运算符,在算术运算符中需要两个操作数来进行运算,而自增自减运算符是一个操作数。

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

    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;//执行完代码前,先自增,在给c赋值
        System.out.println(a);//5
        System.out.println(b);//3
        System.out.println(c);//5
        
        //下面同理
        System.out.println("==============================");

        int a1 = 3;
        int b1 = a1--;
        //a1 = a1-1 //2
        System.out.println(a1);//2
        //a1 = a1-1 
        int c1 = --a1;//1
        System.out.println(a1);//1
        System.out.println(b1);//3
        System.out.println(c1);//1

    }
}

2.逻辑运算

public class demo3 {
    public static void main(String[] args) {
        //与 (and) 或 (or) 非 (取反)
        boolean a = true;
        boolean b = false;
        System.out.println(a && b);//false  //与运算:两个为真,才是true
        System.out.println(a || b);//true  //或运算:一个为真,就是true
        System.out.println(!a);//false   //取反

        System.out.println("=============================================");

        //短路运算
        int c = 5;
        boolean d = (c < 44) && (c++ < 4);
        boolean d1 = (c < 44) || (c++ < 4);
        System.out.println(d);//false
        System.out.println(d1);//true


    }
}

3.位运算

public class demo5 {

    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

        System.out.println(2 << 3);// 16
    }
}

  1. 条件运算符,和扩展运算

    public class demo6 {
        public static void main(String[] args) {
            //条件运算 三元运算
            // x ? y : z
            //如果x==true,结果位y,否则为z
            int tets = 59;
            String type = tets >= 60 ? "及格" : "不及格";
            System.out.println(type);
    
            System.out.println("============================= ");
    
            //扩展运算
            int a = 10;
            int b = 20;
    
            a += b; //a= a+b
            a -= b; //a = a-b
    
            System.out.println(a); //10
    
            //字符串连接符 + ,String
            System.out.println("" + a + b);//1024 ""在前面会将+号后面连接不计算
            System.out.println(a + b + "");//30  ""在后面正常计算
    
    
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值