pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <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="jtnjvnb" 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="pllpjvd" 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="l75bhj7" id='XxvnG'><tfoot id='XxvnG'></tfoot><dl id='XxvnG'><fieldset id='XxvnG'></fieldset></dl></div>

                        • 主站蜘蛛池模板: 发电机价格|发电机组价格|柴油发电机价格|柴油发电机组价格网 | 一航网络-软件测评官网 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 天然鹅卵石滤料厂家-锰砂滤料-石英砂滤料-巩义东枫净水 | 无硅导热垫片-碳纤维导热垫片-导热相变材料厂家-东莞市盛元新材料科技有限公司 | 冻干机(冷冻干燥机)_小型|实验型|食品真空冷冻干燥机-松源 | 不锈钢闸阀_球阀_蝶阀_止回阀_调节阀_截止阀-可拉伐阀门(上海)有限公司 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 万家财经_财经新闻_在线财经资讯网 | 网站建设_网站制作_SEO优化推广_百度推广开户_朋友圈网络科技 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 高压负荷开关-苏州雷尔沃电器有限公司 | 警用|治安|保安|不锈钢岗亭-售货亭价格-垃圾分类亭-移动厕所厂家-苏州灿宇建材 | 成都LED显示屏丨室内户外全彩led屏厂家方案报价_四川诺显科技 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 云南成考网_云南成人高考报名网 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 3d打印服务,3d打印汽车,三维扫描,硅胶复模,手板,快速模具,深圳市精速三维打印科技有限公司 | 防渗土工膜|污水处理防渗膜|垃圾填埋场防渗膜-泰安佳路通工程材料有限公司 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 档案密集架,移动密集架,手摇式密集架,吉林档案密集架-厂家直销★价格公道★质量保证 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 电销卡 防封电销卡 不封号电销卡 电话销售卡 白名单电销卡 电销系统 外呼系统 | elisa试剂盒-PCR试剂盒「上海谷研实业有限公司」 | 生物制药洁净车间-GMP车间净化工程-食品净化厂房-杭州波涛净化设备工程有限公司 | 无轨电动平车_轨道平车_蓄电池电动平车★尽在新乡百特智能转运设备有限公司 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 汕头市盛大文化传播有限公司,www.11400.cc | 卡诺亚轻高定官网_卧室系统_整家定制_定制家居_高端定制_全屋定制加盟_定制家具加盟_定制衣柜加盟 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 扬州汇丰仪表有限公司 | 湖南档案密集架,智能,物证,移动,价格-湖南档案密集架厂家 | 丹佛斯变频器-Danfoss战略代理经销商-上海津信变频器有限公司 | 今日娱乐圈——影视剧集_八卦娱乐_明星八卦_最新娱乐八卦新闻 |