第三節 java 自增,自減,三元運算,短路運算

public class base01 {
    public static void main(String[] args) {
        int m=7;
        int n=7;
        int a=2*++m;//自增運算符,首先先加1,然後再進行運算
        int b=2*n++;//先運算,然後再加1
        System.out.println(a);//16
        System.out.println(b);//14

        System.out.println("------------------");

        int m1=7;
        int n1=7;
        int a1=2*--m1;//自減運算符,首先先減1,然後再進行運算
        int b1=2*n1--;//先運算,然後再減1
        System.out.println(a1);//12
        System.out.println(b1);//14

        //邏輯運算
        System.out.println(1!=1);//false
        System.out.println(1==1);//true

        //三元運算符("1==1 1!=1"爲條件表達式,如果爲true則運行"表達式1",如果爲false則運行"表達式2")
        System.out.println(1==1?"表達式1":"表達式2");//表達式1
        System.out.println(1!=1?"表達式1":"表達式2");//表達式2

        //"與","非","短路"
        System.out.println(1==1 & 2==2);//true  此種情況,兩個條件表達式全部執行,且全部爲true時,才爲true
        System.out.println(1==1 & 2!=2);//false
        System.out.println(1==1 && 2==2);//true 兩個表達式都執行
        System.out.println(1!=1 && 2==2);//false 如果第一個表達式爲false,則不執行第二個表達式,直接爲false


        System.out.println(1==1 | 2==2);//true  此種情況,兩個條件表達式全部執行,且如果有一個爲true時,才爲true
        System.out.println(1==1 | 2!=2);//true  同上
        System.out.println(1==1 || 2==2);//true  如果第一個表達式爲true,則不執行第二個表達式,直接爲true
        System.out.println(1!=1 || 2==2);//true  如果第一個表達式爲false,則執行第二個表達式,如果爲true,直接爲true,反之爲false

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章