久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <tfoot id='l9MK0'></tfoot>
  1. <legend id='l9MK0'><style id='l9MK0'><dir id='l9MK0'><q id='l9MK0'></q></dir></style></legend>
      <bdo id='l9MK0'></bdo><ul id='l9MK0'></ul>
    1. <small id='l9MK0'></small><noframes id='l9MK0'>

      <i id='l9MK0'><tr id='l9MK0'><dt id='l9MK0'><q id='l9MK0'><span id='l9MK0'><b id='l9MK0'><form id='l9MK0'><ins id='l9MK0'></ins><ul id='l9MK0'></ul><sub id='l9MK0'></sub></form><legend id='l9MK0'></legend><bdo id='l9MK0'><pre id='l9MK0'><center id='l9MK0'></center></pre></bdo></b><th id='l9MK0'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='l9MK0'><tfoot id='l9MK0'></tfoot><dl id='l9MK0'><fieldset id='l9MK0'></fieldset></dl></div>

      為什么我的 char 打印為數(shù)字而不是字符?

      Why is my char printing as a number instead of a character?(為什么我的 char 打印為數(shù)字而不是字符?)

    2. <tfoot id='eMK8f'></tfoot>

      <small id='eMK8f'></small><noframes id='eMK8f'>

        <legend id='eMK8f'><style id='eMK8f'><dir id='eMK8f'><q id='eMK8f'></q></dir></style></legend>

            <tbody id='eMK8f'></tbody>
          • <bdo id='eMK8f'></bdo><ul id='eMK8f'></ul>

              <i id='eMK8f'><tr id='eMK8f'><dt id='eMK8f'><q id='eMK8f'><span id='eMK8f'><b id='eMK8f'><form id='eMK8f'><ins id='eMK8f'></ins><ul id='eMK8f'></ul><sub id='eMK8f'></sub></form><legend id='eMK8f'></legend><bdo id='eMK8f'><pre id='eMK8f'><center id='eMK8f'></center></pre></bdo></b><th id='eMK8f'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='eMK8f'><tfoot id='eMK8f'></tfoot><dl id='eMK8f'><fieldset id='eMK8f'></fieldset></dl></div>
                本文介紹了為什么我的 char 打印為數(shù)字而不是字符?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                根據(jù) Java 三元運算符 expression ?語句1:語句2,如果 expression 為 true,則執(zhí)行 statement1,如果 expression 為 false,則執(zhí)行 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.

                但是當(dāng)我跑步時:

                // 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?

                編輯根據(jù) 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?

                如果我執(zhí)行 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.

                所以你的問題真的歸結(jié)為,為什么條件表達式的類型不同

                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 語言規(guī)范第 15.25 節(jié).

                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:

                • 布爾條件表達式
                • 數(shù)值條件表達式
                • 引用條件表達式

                由于 intchar 都可以轉(zhuǎn)換為數(shù)值類型,因此該表達式是根據(jù)此規(guī)則的 數(shù)值條件表達式 的示例:

                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:

                如果第二個和第三個操作數(shù)表達式都是數(shù)值表達式,則條件表達式是數(shù)值條件表達式.

                If both the second and the third operand expressions are numeric expressions, the conditional expression is a numeric conditional expression.

                為了對條件進行分類,以下表達式為數(shù)值表達式:

                For the purpose of classifying a conditional, the following expressions are numeric expressions:

                • 獨立形式的表達式(第 15.2 節(jié)),其類型可轉(zhuǎn)換為數(shù)字類型(第 4.2 節(jié)、第 5.1.8 節(jié)).

                因此,確定整個表達式的類型的規(guī)則如下:

                Given that, the rule for determining the type of the entire expression is given as follows:

                15.25.2.數(shù)值條件表達式

                數(shù)值條件表達式是獨立的表達式(第 15.2 節(jié)).

                Numeric conditional expressions are standalone expressions (§15.2).

                數(shù)值條件表達式的類型確定如下:

                The type of a numeric conditional expression is determined as follows:

                • 如果第二個和第三個操作數(shù)的類型相同,那么就是條件表達式的類型.

                • If the second and third operands have the same type, then that is the type of the conditional expression.

                如果第二個和第三個操作數(shù)之一是原始類型 T,而另一個的類型是對 T 應(yīng)用裝箱轉(zhuǎn)換(第 5.1.7 節(jié))的結(jié)果,則條件表達式的類型為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.

                如果其中一個操作數(shù)是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.

                如果其中一個操作數(shù)是 T 類型,其中 T 是 byte、short 或 char,而另一個操作數(shù)是 int 類型的常量表達式(第 15.28 節(jié)),其值可在類型 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.

                如果其中一個操作數(shù)是 T 類型,其中 T 是 Byte、Short 或 Character,而另一個操作數(shù)是 int 類型的常量表達式,其值可以用 U 類型表示,U 是對T進行拆箱轉(zhuǎn)換,則條件表達式的類型為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.

                否則,二進制數(shù)值提升(第 5.6.2 節(jié))應(yīng)用于操作數(shù)類型,條件表達式的類型是第二個和第三個操作數(shù)的提升類型.

                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.

                請注意,二進制數(shù)值提升執(zhí)行值集轉(zhuǎn)換(第 5.1.13 節(jié))并可能執(zhí)行拆箱轉(zhuǎn)換(第 5.1.8 節(jié)).

                注意第四條規(guī)則準(zhǔn)確地描述了第二個例子;第二個操作數(shù)是 int (0) 類型的常量,第三個是 char,因此條件表達式將計算為 char.這將導(dǎo)致編譯器使用 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.

                但是,當(dāng)您改為傳入 variable 而不是 constant 時,您會陷入最后一條規(guī)則,即...條件表達式的類型是第二個和第三個操作數(shù)的提升類型."

                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 節(jié),它描述了類型提升的規(guī)則如下:

                If you take a look at section §5.6.2 of the JLS, it describes the rules for type promotion as follows:

                當(dāng)運算符對一對操作數(shù)應(yīng)用二進制數(shù)值提升時,每個操作數(shù)都必須表示一個可轉(zhuǎn)換為數(shù)值類型的值,以下規(guī)則按順序適用:

                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:

                1. 如果任何操作數(shù)是引用類型,則對其進行拆箱轉(zhuǎn)換(第 5.1.8 節(jié)).

                1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

                加寬原語轉(zhuǎn)換(第 5.1.2 節(jié))適用于轉(zhuǎn)換以下規(guī)則中指定的一個或兩個操作數(shù):

                Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

                • 如果任一操作數(shù)是 double 類型,則另一個將轉(zhuǎn)換為 double.

                • If either operand is of type double, the other is converted to double.

                否則,如果任一操作數(shù)為浮點類型,則將另一個轉(zhuǎn)換為浮點類型.

                Otherwise, if either operand is of type float, the other is converted to float.

                否則,如果其中一個操作數(shù)是 long 類型,則另一個將轉(zhuǎn)換為 long.

                Otherwise, if either operand is of type long, the other is converted to long.

                否則,兩個操作數(shù)都轉(zhuǎn)換為int類型.

                Otherwise, both operands are converted to type int.

                通過遵循這些規(guī)則,表達式的類型將是 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).

                這篇關(guān)于為什么我的 char 打印為數(shù)字而不是字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用錯誤)
                Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 語句 - 是“或/“和可能的?)
                Java Replace Character At Specific Position Of String?(Java替換字符串特定位置的字符?)
                What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作數(shù)的三元表達式的類型是什么?)
                Read a text file and store every single character occurrence(讀取文本文件并存儲出現(xiàn)的每個字符)
                Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉(zhuǎn)換 char 原語?)
                  <tbody id='XxvnG'></tbody>

                    <tfoot id='XxvnG'></tfoot>
                  1. <small id='XxvnG'></small><noframes id='XxvnG'>

                    <legend id='XxvnG'><style id='XxvnG'><dir id='XxvnG'><q id='XxvnG'></q></dir></style></legend>
                      <bdo id='XxvnG'></bdo><ul id='XxvnG'></ul>
                        • <i id='XxvnG'><tr id='XxvnG'><dt id='XxvnG'><q id='XxvnG'><span id='XxvnG'><b id='XxvnG'><form id='XxvnG'><ins id='XxvnG'></ins><ul id='XxvnG'></ul><sub id='XxvnG'></sub></form><legend id='XxvnG'></legend><bdo id='XxvnG'><pre id='XxvnG'><center id='XxvnG'></center></pre></bdo></b><th id='XxvnG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='XxvnG'><tfoot id='XxvnG'></tfoot><dl id='XxvnG'><fieldset id='XxvnG'></fieldset></dl></div>

                        • 主站蜘蛛池模板: 男女视频在线免费观看 | 免费一区二区三区 | 久草免费电影 | 国产成人免费视频网站视频社区 | 男人的天堂中文字幕 | 久久精品亚洲精品国产欧美 | 国产高清一区二区三区 | 成人亚洲综合 | 国产a级毛毛片 | 看av网 | 羞羞视频在线观看 | 亚洲一区精品在线 | 国产精品免费一区二区三区四区 | 国产一区二| 日日做夜夜爽毛片麻豆 | 免费毛片在线 | 性色视频 | 国产亚洲精品精品国产亚洲综合 | 日本久久精品视频 | 中文字幕在线视频网站 | 欧美成年黄网站色视频 | 少妇性l交大片免费一 | 久久久123| 亚洲精品乱码久久久久久久久 | 日日噜 | 一区二区三区欧美 | 中文字幕一二三 | 欧美成人a∨高清免费观看 色999日韩 | 久久婷婷国产麻豆91 | 亚洲精品女人久久久 | 81精品国产乱码久久久久久 | www.com久久久| 日韩欧美一区二区三区免费看 | 成人国产精品久久 | 国产毛片久久久 | 91麻豆精品国产91久久久久久 | 91精品国产手机 | 久久精品国产一区 | 51ⅴ精品国产91久久久久久 | 北条麻妃99精品青青久久主播 | 欧美日韩亚|