三目運算符運算順序

public class Test {
    public static void main(String[] args) {
       
        Object o1=true? new Integer(1): new Double(2.0);
        Object o2;
        if(true){
            o2=new Integer(1);
        }else {
            o2=new Double(2.0);
        }
        System.out.println(o1);
        System.out.println(" ");
        System.out.println(o2);
    }

}

三元運算符優先級----高於賦值運算符相當於
Object o1=(true? new Integer(1): new Double(2.0));
三元運算符之間的類型自動從低往高轉換Integer自動轉換爲Double
最後輸出1.0 1

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