三目运算符运算顺序

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

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