Java运算符二

逻辑运算符

  • 与(and)
  • 或(or)
  • 非(取反)

逻辑运算符代码一:

package operator;
//逻辑运算符
public class Demon05 {
    public static void main(String[] args) {
        //与(and) 或(or) 非(取反)
        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 = 5;
        boolean d = (c<4)&&(c++<10);  //因为(c<4)为false,则不执行后面(c++<10)
        System.out.println(d);
        System.out.println(c);
    }
}

逻辑运算符运行结果一:

D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60102:D:\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon05
a && bfalse
a || btrue
!(a && b)true
false
5

Process finished with exit code 0

位运算符代码二:

package operator;

public class Demon06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
        --------------------------------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001 //异或:相同为0,不同为1
        ~B = 1111 0010
        //在计算机里边2*8,如何计算最快:位运算(效率极高)
        2*8 = 16------>  2*2*2*2
        ---------------------------------------
        <<:左移  *2
        >>:右移  /2
        0000 0000   0
        0000 0001   1
        0000 0010   2
        0000 0100   4
        0000 1000   8
        0001 0000   16
         */
        System.out.println(2<<3);
    }
}

位运算符运行结果二:

D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60110:D:\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon06
16

Process finished with exit code 0

字符串连接符代码三:

package operator;

public class Demon07 {
    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);
        //字符串连接符:  +  ,String(在+运算符两边,如果有一方出现了字符串类型,就会把其他操作数都转换成String在链接)
        System.out.println(a+b);
        System.out.println(""+a+b);  //1020  字符串在前边,后面会链接
        System.out.println(a+b+"");  //30    字符串如果在后面,前面的会计算
    }
}

字符串连接符运行结果三:

D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60142:D:\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon07
30
1020
30

Process finished with exit code 0

三元运算符代码四

package operator;
//三元运算符
public class Demon08 {
    public static void main(String[] args) {
        //  x ? y :z
        //如果x=true,结果为y,否则结果为z

        int score = 50;
        String tyep = score <60 ?"不及格":"及格"; //必须掌握
        // if
        System.out.println(tyep);
    }
}

三元运算符运行结果四:

D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60170:D:\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon08
不及格

Process finished with exit code 0

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值