Java基础语法(七)

运算符(二)

逻辑运算符

package operator;

//逻辑运算符
public class Demo05 {
     
    public static void main(String[] args) {
     
        //与 && (and) 或 || (or) 非 !
        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);
        System.out.println(c);
    }
}

左移右移

package operator;

public class Demo06 {
     
    public static void main(String[] args) {
     
        //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-=b;

        //字符串连接符 + ,string
        System.out.println(""+a+b);
        System.out.println(a+b+"");

    }
}

三元运算符

package operator;

public class Demo08 {
     
    public static void main(String[] args) {
     
        int score = 80;
        String type = score <60 ? "不及格" : "及格";
        System.out.println(type);
    }

}

优先级

Java基础语法(七)_第1张图片

点击跳转到遇见狂神说视频教程

你可能感兴趣的:(学习,java)