关于三元运算符的疑问!求指点
public class GenericTest{
public static void main(String[] args){
char a = 'A';
int i = 0;
System.out.println(true ? 'A' : i);
System.out.println(true ? 'A' : 0);
System.out.println(true ? a : i);
System.out.println(true ? a : 0);
System.out.println(false ? 0 : a);
System.out.println(false ? 0 : 'A');
System.out.println(false ? i : a);
System.out.println(false ? i : 'A');
}
}
【输出结果】
65
A
65
A
A
A
65
65
毫无规律啊,实在不能理解为什么是这些输出!求大神指教!三元运算符的转型规则!~ 转型 三元运算符
[解决办法]
The type of a conditional expression is determined as follows:
? If the second and third operands have the same type (which may be the null type),
then that is the type of the conditional expression.
? If one of the second and third operands is of primitive type T, and the type of the
other is the result of applying boxing conversion (§5.1.7) to T, then the type of
the conditional expression is T.
? If one of the second and third operands is of the null type and the type of the other
is a reference type, then the type of the conditional expression is that reference
type.
? Otherwise, if the second and third operands have types that are convertible
(§5.1.8) to numeric types, then there are several cases:
◆ If one of the operands is of type byte or Byte and the other is of type short
or Short, then the type of the conditional expression is short.
◆ If one of the operands is of type T where T is byte, short, or char, and the
other operand is a constant expression (§15.28) of type int whose value is
representable in type T, then the type of the conditional expression is T.
If one of the operands is of type T, where T is Byte, Short, or Character, and
the other operand is a constant expression (§15.28) of type int whose value is
representable in the type U which is the result of applying unboxing conversion
to T, then the type of the conditional expression is U.
◆ Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types,
and the type of the conditional expression is the promoted type of the second
and third operands.
Note that binary numeric promotion performs value set conversion (§5.1.13) and may
perform unboxing conversion (§5.1.8).
? Otherwise, the second and third operands are of types S1 and S2 respectively. Let
T1 be the type that results from applying boxing conversion to S1, and let T2 be
the type that results from applying boxing conversion to S2.
The type of the conditional expression is the result of applying capture
conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).
[解决办法]
楼上已经说过了。
以下几行摘自《java解惑》,其实是对jls中的一段(也就是楼上贴出来的)的翻译,比较绕。
如果第二个和第三个操作数具有相同的类型,那么它就是条件表达式的类型。换句话说,你可以通过绕过混合类型的计算来避免大麻烦。
如果一个操作数的类型是T,T表示byte、short、或者char,而另一个操作数是一个int类型的常量表达式,它的值是可以用类型T表示的,那么条件表达式的类型就是T。
否则,将对操作数类型运用二进制数字提升,而条件表达式的类型就是第二个和第三个操作数呗提升之后的类型。
详情请看:JLS7之15.25。
[解决办法]
一、分析
当你使用三元运算符,两边的操作数的类型不一致的时候,这就涉及到三元操作符的转换规则:
1.若果两个操作数不可转换,则不做转换,返回值为Object类型。
2.若两个操作数是明确类型的表达式(比如变量),则按照正常的二进制数字来转换。int类型转换为long类型,long类型转换成float类型。
3.若两个操作数中有一个是数字S,另外一个是表达式,且其类型为T,那么,若数字S在T的范围内,则转换为T类型;若S超过了T的范围,则T转换为S类型。
4.若两个操作数字都是直接数字。则返回值类型为范围较大者。
二、场景
[plain] view plaincopy
public class Client{
public static void main(String[] args){
int i = 80;
String s = String.valueOf(i < 100? 90 : 100);
String s1 = String.valueOf(i < 100? 90 : 100.0);
System.out.println("两者是否相等:" + s.equals(s1)):
}
}
分析,两个三元操运算,条件都为真,返回第一个值,结果“两者相等:true”。结果果真如此吗?结果“两者相等:false”!
问题出在于100和100.0这两个数字上:
在变量s中,第一个操作数(90)和第二个操作数(100)都是int类型,类型相同,返回的是int型的90;
在变量s1中,第一个操作数类型为(90)int类型,第二个操作数是(100.0)浮点型。
可是三元操作符必须返回同一个数据,而且类型要确定,不可能条件为真返回int类型,条件为假返回float类型,编译器是不会允许的,所以进行类型转换了。int转换成90.0,也就是所返回值是90.0当然和90不相等了。
三、建议
保证三元操作符中的两个操作类型一致,即可减少错误的发生。