久久久久久久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 打印為數字而不是字符?

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

    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 打印為數字而不是字符?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                根據 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:

                • 布爾條件表達式
                • 數值條件表達式
                • 引用條件表達式

                由于 intchar 都可以轉換為數值類型,因此該表達式是根據此規則的 數值條件表達式 的示例:

                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:

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

                1. 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模板網!

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

                相關文檔推薦

                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 操作數的三元表達式的類型是什么?)
                Read a text file and store every single character occurrence(讀取文本文件并存儲出現的每個字符)
                Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉換 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>

                        • 主站蜘蛛池模板: 国产伦精品一区二区三区高清 | 亚洲精品一区在线 | 在线午夜 | 国产亚洲成av人片在线观看桃 | 欧美一级在线观看 | 久久黄网 | 久久精品99国产精品日本 | 久草热线 | 毛片免费在线 | 日本在线观看网址 | 精品在线99 | 国产精品二区三区在线观看 | 欧美一级片在线 | 国产免费视频 | 色综合一区二区三区 | 91美女在线观看 | 亚洲精品日韩在线观看 | 羞羞色网站 | 国产日韩欧美91 | 成人免费视频网站在线观看 | 天天色影视综合 | 久久久久国 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 日韩在线精品视频 | 久久亚洲国产精品日日av夜夜 | 国产高清一区二区三区 | 99久久婷婷国产综合精品电影 | 欧美精品三区 | 性生活毛片 | aaaaaaa片毛片免费观看 | 欧美三级在线 | 高清久久久 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 国产成人精品区一区二区不卡 | 免费av手机在线观看 | 中文字幕一区二区三区精彩视频 | 国产三区四区 | 91久久精品| 91久久久久 | 国产精品第2页 | 亚洲国产精品成人无久久精品 |