简体   繁体   English

条件赋值中的数字推广如何在Java中工作

[英]How numeric promotion in a conditional assignment works in Java

Found some strange things in java. 在java中发现了一些奇怪的东西。

Code: 码:

    System.out.println(System.getProperty("java.version"));
    System.out.println((true) ? (int)2.5 : 3.5);
    System.out.println((true) ? (int)2.5 : 3);
    System.out.println((true) ? (int)2.5 + "" : 3.5);

Result: 结果:

1.8.0_40
2.0
2
2

What is it? 它是什么? Why integer value returns only if value for false is not a double or if string value added to value for true? 为什么只有当false的值不是double或者如果将字符串值添加到true的值时,整数值才会返回? Is it a bug? 这是一个错误吗?

This specification documents the numeric promotion in Java. 规范记录了Java中的数字提升。 In section 2, it says that if either operand is of type double then the result is of type double. 在第2节中,它表示如果任一操作数的类型为double则结果为double类型。 In this specification (which was added by @Januson), it is mentioned that: 在本规范 (由@Januson添加)中,提到:

the type of the conditional expression is the promoted type of the second AND third operands 条件表达式的类型是第二个第三个操作数的提升类型

Now, if we apply those two documents to our examples, we get the following: 现在,如果我们将这两个文档应用于我们的示例,我们将得到以下内容:

System.out.println(System.getProperty("java.version"));

This is the version (release) of Java. 这是Java的版本(发行版)。 It is a String . 这是一个String

System.out.println((true) ? (int)2.5 : 3.5);

In this case the compiler looks at the both parts of the conditional and decides the data type. 在这种情况下,编译器会查看条件的两个部分并确定数据类型。 (int)2.5 is an integer and 3.5 is a double . (int)2.5是integer ,3.5是double The result will be a double . 结果将是double So the integer part of 2.5 (ie, 2) will be cast to a double . 所以2.5(即2)的整数部分将被转换为double

System.out.println((true) ? (int)2.5 : 3);

In this case, the compiler looks at 2 integers so the result is an integer . 在这种情况下,编译器查看2个整数,因此结果是integer The first case gives an integer and the second case is also an integer so the result will be in integer . 第一种情况给出一个integer ,第二种情况也是一个integer因此结果将是integer

System.out.println((true) ? (int)2.5 + "" : 3.5);

In this case, the compiler looks at the first case and takes the integer part which is 2. Now, because this number is appended to a string, it becomes a String . 在这种情况下,编译器查看第一种情况并获取整数部分,即2.现在,因为此数字附加到字符串,它将变为String The result will also be a string. 结果也将是一个字符串。 If you try to return 3.5, this will be returned as a String and not a double . 如果您尝试返回3.5,则将以String形式返回,而不是double

Because of the way ternary operator resolves its return type. 由于三元运算符解析其返回类型的方式。 Check its specification for more information. 检查其规范以获取更多信息。

First expression returns double because 2.5 is first converted to int 2 but since third argument is double and int is convertible to double then return type for whole expression is double 2.0 第一个表达式返回double,因为2.5首先转换为int 2,但由于第三个参数是double而int可以转换为double,因此整个表达式的返回类型是double 2.0

Second return symply int 2 because 2.5 to int is 2 and return type is int. 第二次返回symply int 2因为2.5到int是2而返回类型是int。

Third first converts 2.5 to 2, then to String "2" and return type is String. 第三个首先将2.5转换为2,然后转换为字符串“2”,返回类型为String。

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

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