基础-基本运算

基本运算

  • 与C类似

拓展(以Math类为例)

package operator;

public class Demo02 {
    public static void main(String[] args) {
        
        //很多运算,我们会使用一些工具类来操作
        double pow;
        pow=Math.pow(2, 3);
        System.out.println(pow);
    }
}

逻辑运算&&短路运算

  • 短路keys:若a&&b中a为false,则不运行b

代码

package operator;

//逻辑运算符
public class Demo03 {
    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));
        System.out.println("==================================");

        //短路运算
        int c=5;
        boolean d=(c<4)&&(++c<4);
        System.out.println(d);
        System.out.println(c);
    }
}

posted @ 2021-12-02 21:10  梧桐灯下江楚滢  阅读(31)  评论(0编辑  收藏  举报