为什么三元运算符意外地转换整数? [英] Why does the ternary operator unexpectedly cast integers?

查看:112
本文介绍了为什么三元运算符意外地转换整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到它讨论了下面的代码导致 obj 是一个,但它打印 200.0 从左侧。

  Object obj = true? new Integer(200):new Double(0.0); 

System.out.println(obj);

结果:200.0






但是,如果你把一个不同的对象放在右边,例如 BigDecimal obj 的类型是 Integer be。

  Object obj = true? new Integer(200):new BigDecimal(0.0); 

System.out.println(obj);

结果:200






我假设这个原因与将 double 的左边转换为与 integer / double 比较和计算,但这里的左侧和右侧不以这种方式交互。

为什么会发生这种情况?

解决方案

您需要阅读 Java语言规范的第15.25节。



特别是:


否则,如果第二个和第三个操作数有可转换的类型(§5.1.8)类型,则有几种情况:




  • 如果其中一个操作数的类型为byte或Byte,另一个类型为short或Short

  • 如果其中一个操作数是类型T,其中T是byte,short或char,另一个操作数是一个常数表达式类型int,其值在类型T中可表示,则> - 条件表达式的类型为T.

  • 如果其中一个操作数的类型为Byte,另一个操作数是常量表达式

  • 如果其中一个操作数的类型为Short,另一个操作数是一个常量表达式,则该类型为int,其值在类型字节中表示,则该条件表达式的类型为byte。类型int,其值在类型short中可表示,则条件表达式的类型很短。

  • 如果其中一个操作数的类型为<字符和其他操作数是类型int的常量表达式,其值在类型char中可表示,则条件表达式的类型为char。

  • 否则,二进制数字促销(§5.6。 2)应用于操作数类型,条件表达式的类型是第二和第三操作数的提升类型。注意,二进制数字促销执行拆箱转换(§5.1.8)和值集转换(§5.1.13)。


因此,应用了二进制数字促销开头为:


当运算符将二进制数字提升应用于一对操作数时,每个操作数必须表示可转换为使用加宽转换(§5.1.2),根据需要转换操作数:




  • 如果有的操作数是参考类型,则执行拆箱转换(§5.1.8)。然后:

  • 如果任一操作数的类型为double,则另一个转换为double。


这是发生在这里 - 参数类型转换为 int double 分别,第二个操作数(原始表达式中的第三个)的类型为 double ,因此总体结果类型为 double


I have seen it discussed somewhere that the following code results in obj being a Double, but that it prints 200.0 from the left hand side.

Object obj = true ? new Integer(200) : new Double(0.0);

System.out.println(obj);

Result: 200.0


However, if you put a different object in the right hand side, e.g. BigDecimal, the type of obj is Integer as it should be.

Object obj = true ? new Integer(200) : new BigDecimal(0.0);

System.out.println(obj);

Result: 200


I presume that the reason for this is something to do with casting the left hand side to a double in the same way that it happens for integer/double comparisons and calculations, but here the left and right sides do not interact in this way.

Why does this happen?

解决方案

You need to read section 15.25 of the Java Language Specification.

In particular:

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 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 Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.
  • If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.
  • If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.
  • 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 unboxing conversion (§5.1.8) and value set conversion (§5.1.13).

So binary numeric promotion is applied, which starts with:

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:

  • If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:
  • If either operand is of type double, the other is converted to double.

That's exactly what happens here - the parameter types are converted to int and double respectively, the second operand (the third in the original expression) is then of type double, so the overall result type is double.

这篇关于为什么三元运算符意外地转换整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆