Java三元表达式有字符强转的功能,返回值类型为两个返回值中类型精度更高的那个类型。
有浮点数参与运算,结果应该也是浮点数
System.out.println("value is " + (x > 4 ? 99.9 : 9));
等价代码如下:
double num = x > 4 ? 99.9 : 9;
System.out.println("value is " + num);
字符串拼接的是 double 类型,因此显示了小数点
三元运算的数据类型要一样,99.9 浮点型和9 整型要转为同一大类型,int类型的9会转为浮点类型的9.0
https://blog.csdn.net/liuwenliang_002/article/details/79003367