运算符

运算符

算术运算符

package operator;

public class Demo1 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl+D :可以复制当前行到下一行
        int a = 10;
        int b = 22;
        int c = 30;
        int d = 40;

        System.out.println(a+b);
        System.out.println(b%a); //取余,模运算 22 %10 =2
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b); //有小数的时候注意转换
    }
}

package operator;

public class Demo2 {
    public static void main(String[] args) {
        long a = 12345643L;
        int b = 245;
        double c = 45142.5;
        short d = 34;
        byte e = 8;

        System.out.println(a+b+c+d+e);//1.23910725E7,科学计数法,double类型 ,8位有效数据位,E 后面数字是几,就是有效位数乘上10的几次方
        System.out.println(a+c); //1.23907855E7
        System.out.println(a+b+d+e);//12345930
        System.out.println(b+c+d+e); //45429.5
        System.out.println(c+d+e); //45184.5
        System.out.println(d+e); //42

        /*有double型,输出double型
        有long型,输出long型
        其他都是输出int型
        如果有小数,注意转换
         */

    }
}

package operator;

public class Demo4 {
    public static void main(String[] args) {
        //算术运算符 ++  --   自增  自减  一元运算符

        int a = 3;
        int b = a++;//先把a的值赋给b再自增
        System.out.println(a);//4
        int c = ++a;//先自增再把a的值赋给c

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

        //幂运算,用一个数学类Math,很多运算都是用到这个类
        double pow = Math.pow(2,3);
        System.out.println(pow);//8.0

    }
}

关系运算符

package operator;

public class Demo3 {
    public static void main(String[] args) {
        //关系运算符,返回的结果是布尔值,true or false,经常跟if一起搭配使用

        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
    }
}

逻辑运算符

package operator;

public class Demo5 {
    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(d); //false,所以后面的c++就不执行,c还是等于5
        System.out.println(c); //5
    }
}

位运算

package operator;

public class Demo6 {
    public static void main(String[] args) {
        //位运算
        /*
        A = 0011 1100
        B = 0000 1101
        ---------------------------
        A&B = 0000 1100  与
        A|B = 0011 1101  或
        A^B = 0011 0001  异或
        ~A =  1100 0011  非
         效率极高!!!
        << 左移 相当于*2
        >> 右移 相当于/2
         */
        // 2*8 = 2*2*2*2
        System.out.println(2<<3);//16
    }
}

扩展赋值运算符&&字符串连接符

package operator;

public class Demo7 {
    public static void main(String[] args) {
        //扩展赋值运算符(偷懒运算符)
        int a = 10;
        int b = 20;

        a+=b;
        System.out.println(a); //30
        a-=b;
        System.out.println(a); //10

        //字符串连接符:+ , String
        System.out.println(""+a+b); //1020
        System.out.println(a+b+""); //30



    }
}

条件运算符

package operator;

public class Demo8 {
    public static void main(String[] args) {
        /*三元运算符:条件运算符 ?:
        x ? y : z
        如果x==true,结果为y,否则结果为z
        必须掌握
         */

        int score = 90;
        String type = score < 60 ? "fail" : "pass";
        System.out.println(type); //pass

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值