根据 Java 三元运算符 expression ?语句1:语句2
,如果 expression
为 true,则执行 statement1
,如果 expression
为 false,则执行 statement2
.
As per the Java ternary operator expression ? statement1 : statement2
,
if expression
is true then statement1
will be executed, if expression
is false then statement2
will be executed.
但是当我跑步时:
// some unnecessary codes not displaying
char y = 'y';
int i = 0;
System.out.print(false ? i : y);
我希望它打印 y
但它打印 121
,为什么?
I am expecting it to print y
but its printing 121
, why?
编辑根据 manouti 的回答,编译器解释为 int
,但如果是这种情况,那么为什么我会在 i
处看到死代码?
EDIT
As per the manouti answer, the compiler interprets as int
, but if that is the case then why I am seeing dead code at i
?
如果我执行 System.out.print(false ? 0 : x);
那么我得到 y
,那么为什么在这种情况下编译器不解释作为 int
?
If I do System.out.print(false ? 0 : x);
then I am getting y
, so why in this case doesn't the compiler interpret as int
?
您的问题的简短回答是打印的值基于条件表达式计算的类型.
The short answer to your question is that the value printed is based on the type that the conditional expression evaluates to.
所以你的问题真的归结为,为什么条件表达式的类型不同
So really your question boils down to, why does the type of the conditional expression differ between
char y = 'y';
int i = 0;
System.out.print(false ? i : y); // prints 121
和
char y = 'y';
System.out.print(false ? 0 : y); // prints y
要回答这个问题,我们需要看看 Java 语言规范第 15.25 节.
To answer that, we'll need to take a look at section §15.25 of the Java Language Specification.
Java 中的条件表达式分为三种:
There are three types of conditional expression in Java:
由于 int
和 char
都可以转换为数值类型,因此该表达式是根据此规则的 数值条件表达式 的示例:
Since both int
and char
are convertible to a numeric type, the expression is an example of a numeric conditional expression according to this rule:
如果第二个和第三个操作数表达式都是数值表达式,则条件表达式是数值条件表达式.
If both the second and the third operand expressions are numeric expressions, the conditional expression is a numeric conditional expression.
为了对条件进行分类,以下表达式为数值表达式:
For the purpose of classifying a conditional, the following expressions are numeric expressions:
因此,确定整个表达式的类型的规则如下:
Given that, the rule for determining the type of the entire expression is given as follows:
15.25.2.数值条件表达式
数值条件表达式是独立的表达式(第 15.2 节).
Numeric conditional expressions are standalone expressions (§15.2).
数值条件表达式的类型确定如下:
The type of a numeric conditional expression is determined as follows:
如果第二个和第三个操作数的类型相同,那么就是条件表达式的类型.
If the second and third operands have the same type, then that is the type of the conditional expression.
如果第二个和第三个操作数之一是原始类型 T,而另一个的类型是对 T 应用装箱转换(第 5.1.7 节)的结果,则条件表达式的类型为T.
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.
如果其中一个操作数是byte或Byte类型,另一个是short或Short类型,则条件表达式的类型为short.
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.
如果其中一个操作数是 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 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 或 Character,而另一个操作数是 int 类型的常量表达式,其值可以用 U 类型表示,U 是对T进行拆箱转换,则条件表达式的类型为U.
If one of the operands is of type T, where T is Byte, Short, or Character, and the other operand is a constant expression 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.
否则,二进制数值提升(第 5.6.2 节)应用于操作数类型,条件表达式的类型是第二个和第三个操作数的提升类型.
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.1.13 节)并可能执行拆箱转换(第 5.1.8 节).
注意第四条规则准确地描述了第二个例子;第二个操作数是 int
(0
) 类型的常量,第三个是 char
,因此条件表达式将计算为 char代码>.这将导致编译器使用
print(char)
方法,该方法将打印 y
.
Notice that the fourth rule exactly describes the second example; the second operand is constant of type int
(0
) and the third is a char
, so the conditional expression will evaluate to char
. This will cause the compiler to use the print(char)
method, which will print y
.
但是,当您改为传入 variable 而不是 constant 时,您会陷入最后一条规则,即...条件表达式的类型是第二个和第三个操作数的提升类型."
However when you instead pass in a variable instead of a constant, you fall down to the last rule which says that "...the type of the conditional expression is the promoted type of the second and third operands."
如果你看看 JLS §5.6.2 节,它描述了类型提升的规则如下:
If you take a look at section §5.6.2 of the JLS, it describes the rules for type promotion as follows:
当运算符对一对操作数应用二进制数值提升时,每个操作数都必须表示一个可转换为数值类型的值,以下规则按顺序适用:
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:
如果任何操作数是引用类型,则对其进行拆箱转换(第 5.1.8 节).
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
加宽原语转换(第 5.1.2 节)适用于转换以下规则中指定的一个或两个操作数:
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
如果任一操作数是 double 类型,则另一个将转换为 double.
If either operand is of type double, the other is converted to double.
否则,如果任一操作数为浮点类型,则将另一个转换为浮点类型.
Otherwise, if either operand is of type float, the other is converted to float.
否则,如果其中一个操作数是 long 类型,则另一个将转换为 long.
Otherwise, if either operand is of type long, the other is converted to long.
否则,两个操作数都转换为int类型.
Otherwise, both operands are converted to type int.
通过遵循这些规则,表达式的类型将是 int
,因此编译器将使用 print(int)
方法,该方法将打印 121
(y
的ascii值).
By following these rules, the type of the expression will be int
, and so the compiler will use the print(int)
method, which will print 121
(the ascii value of y
).
这篇关于为什么我的 char 打印为数字而不是字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!