1 public class SopResult { 2 3 public static void main(String[] args) { 4 5 int i = 4; 6 System.out.println("The result is: " + ((i > 4) ? 9.99 : 9)); 7 } 8 9 }
读程序,输出结果:The result is: 9.0
分析可知,本程序相当于:
1 public class SopResult { 2 3 public static void main(String[] args) { 4 5 int i = 4; 6 double result = (i > 4) ? 9.99 : 9; 7 System.out.println("The result is: " + result); 8 } 9 10 }
由于9.99为double类型,因此返回结果类型应为double类型
相当于double result = 9;
所以输出结果为The result is: 9.0