問題描述
根據 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:
- 獨立形式的表達式(第 15.2 節),其類型可轉換為數字類型(第 4.2 節、第 5.1.8 節).
因此,確定整個表達式的類型的規則如下:
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 打印為數字而不是字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!