首页 > 编程语言 > 详细

JAVA学习(运算符二)

时间:2020-06-08 14:20:09      阅读:30      评论:0      收藏:0      [点我收藏+]
package operator;
//逻辑运算符 && (与)、||(或)、!(非)
public class Demo05 {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        System.out.println("a && b:"+(a&&b));
        System.out.println("a || b:"+(a||b));
        System.out.println("!(a && b):"+!(a&&b));
        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);//false
        System.out.println(c);//5
    }
}
package operator;

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

        System.out.println();
        /*
        A = 0011 1100
        B = 0000 1101

        A&B 0000 1100
        A/B 0011 1101
        A^B 0011 0001  相同取0,不同得1
        ~A  1100 0011  取反

        2*8 怎么运算最快?
        <<  *2
        >>  /2
         */
        System.out.println(2<<3);
    }
}
package operator;

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
        System.out.println(a);//10

        //字符串连接符  +
        System.out.println(a+b); //30
        System.out.println(""+a+b); //1020 只要不在最末尾,都会把其余转化为String
        System.out.println(a+b+""); //30
    }
}
package operator;

//三元运算符
public class Demo8 {
    public static void main(String[] args) {
        // x?y:z
        //x ture 结果为y,否则结果为z
        int score = 80;
        String type = score < 60 ?"不及格":"及格"; //必须掌握

        System.out.println(type);
    }
}

 

JAVA学习(运算符二)

原文:https://www.cnblogs.com/cjybarcode/p/13065299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!