简体   繁体   English

三元运算符

[英]Ternary operator

Why the output of following code is 9.0 and not 9 ? 为什么以下代码的输出是9.0而不是9? If ternary operator is nothing but short form of if-else branch then why java compiler is promoting int to double ? 如果三元运算符只是if-else分支的简短形式,那么为什么java编译器将int提升为double?

public class Ternary
  {
public static void main(String args[])
     {
    int a = 5;
    System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));
     }
  }

If ternary operator is nothing but short form of if-else branch then why java compiler is promoting int to double ? 如果三元运算符只是if-else分支的简短形式,那么为什么java编译器将int提升为double?

A conditional expression has a single type, which both the second and third operands are converted to as necessary. 条件表达式具有单一类型,第二个和第三个操作数都将根据需要进行转换。 The JLS gives the rules determining the expression type, which are slightly complicated due to auto-unboxing. JLS提供了确定表达式类型的规则,由于自动取消装箱,这些规则稍微复杂一些。

The conditional operator is sort of just shorthand for an if / else construct, but not the sort of shorthand I think you expected. 条件运算符是那种只是速记的if / else构造,但不是那种速记的我想你的预期。 So your code is equivalent to this: 所以你的代码等同于:

double value;
if (a < 5) {
    value = 9.9;
} else {
    value = 9;
}
System.out.println("Value is - " + value);

It's not short for: 不是简短的:

if (a < 5) {
    System.out.println("Value is - " + 9.9);
} else {
    System.out.println("Value is - " + 9);
}

For more details, see section 15.25 of the Java Language Specification . 有关更多详细信息,请参阅Java语言规范的15.25节

Because the type of the conditional operator (Yes, it's conditional operator and not ternary operator ) in this case will be the promoted type of the 3rd operand, since 2nd and 3rd operand are not of same type. 因为在这种conditional operator的类型(是,它是条件运算符而不是三元运算符 )将是第3个操作数的提升类型,因为第2和第3个操作数不是同一类型。

This is clearly listed in JLS Secion - 15.25 : - 这清楚地列在JLS Secion - 15.25中 : -

Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases: 否则,如果第二个和第三个操作数具有可转换的类型(第5.1.8节)到数字类型,则有几种情况:

  • 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. 如果其中一个操作数的类型为字节或字节,另一个操作数的类型为short或Short,则则条件表达式的类型为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. 如果其中一个操作数是T类型,其中T是byte,short或char,另一个操作数是int类型的常量表达式(第15.28节),其值可以在类型T中表示,那么条件表达式的类型是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. 如果其中一个操作数是T类型,其中T是Byte,Short或Character,另一个操作数是int类型的常量表达式(第15.28节),其值可以在类型U中表示,这是应用拆箱的结果转换为T,则条件表达式的类型为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. 否则,二进制数字提升(第5.6.2节)将应用于操作数类型,条件表达式的类型是第二个和第三个操作数的提升类型。

See the last point, that is of use here. 请参阅最后一点,这是有用的。 So, in this case, as a rule of binary numeric promotion - See JLS Section 5.6.2 : - 因此,在这种情况下,作为binary numeric promotion的规则 - 参见JLS第5.6.2节 : -

  • If either operand is of type double, the other is converted to double. 如果任一操作数的类型为double,则另一个操作数转换为double。

Because the type of the expression as a whole is double , because one of the operands to the operator is a double . 因为整个表达式的类型是double ,因为运算符的一个操作数是double The type of the expression containing the ternary is dictated by the operands, which must be of the same type. 包含三元组的表达式的类型由操作数决定,操作数必须是相同的类型。 In the case of your expression, the 9 is coerced to a double to make it the same type as the 9.9 . 在表达式的情况下, 9被强制为double ,使其与9.9相同。

Actually the ternary operator is not strictly speaking a short form of if/else as it does perform type conversion if required. 实际上,三元运算符并不严格地说是if / else的简短形式,因为它在需要时执行类型转换。 In particular, in your case, JLS 15.25 requires that: 特别是在您的情况下, JLS 15.25要求:

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. 二进制数字提升(第5.6.2节)应用于操作数类型,条件表达式的类型是第二个和第三个操作数的提升类型。

If you follow the link to §5.6.2 : 如果您点击§5.6.2的链接:

If either operand is of type double, the other is converted to double. 如果任一操作数的类型为double,则另一个操作数转换为double。

Java needs to know the type of the result at compile time. Java需要在编译时知道结果的类型。 So as this ternary operator can result an int or a double, the compiler chooses the double as the result type. 因此,当这个三元运算符可以产生int或double时,编译器选择double作为结果类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM