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

  1. <tfoot id='8oGkn'></tfoot>

      • <bdo id='8oGkn'></bdo><ul id='8oGkn'></ul>

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

    2. <legend id='8oGkn'><style id='8oGkn'><dir id='8oGkn'><q id='8oGkn'></q></dir></style></legend>

    3. Java 中的隱式轉(zhuǎn)換是如何工作的?

      How does implicit conversion work in Java?(Java 中的隱式轉(zhuǎn)換是如何工作的?)
    4. <tfoot id='Z1idh'></tfoot>

        • <bdo id='Z1idh'></bdo><ul id='Z1idh'></ul>

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

                  <tbody id='Z1idh'></tbody>
                <legend id='Z1idh'><style id='Z1idh'><dir id='Z1idh'><q id='Z1idh'></q></dir></style></legend>

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

                本文介紹了Java 中的隱式轉(zhuǎn)換是如何工作的?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我知道在 Java 中整數(shù)文字默認(rèn)是 int,所以如果我寫這樣的東西

                I know that in Java Integer literals are int by default,so if I write something like this

                byte byteValue = 2;
                

                Java 自動將文字值 2(默認(rèn)為 int)轉(zhuǎn)換為字節(jié).如果我寫,同樣的事情也有效

                Java auto converts the literal value 2(which is an int by default) to byte. And the same thing works if I write

                byte byteValue = 4/2;
                

                RHS 被評估為 int 并隱式轉(zhuǎn)換為字節(jié).

                The RHS is evaluated as an int and implicitly converted to a byte.

                但是為什么下面兩種情況沒有發(fā)生隱式轉(zhuǎn)換呢?

                int n1 = 4;
                byte value = n1/2;
                

                或在此

                byte n1 = 4;
                byte value = n1/2;
                

                我知道這兩個示例的 RHS 都被評估為 int.但是為什么Java不像前兩種情況那樣將其隱式轉(zhuǎn)換為字節(jié).是否只有在有字面量的情況下才會隱式轉(zhuǎn)換為較小的數(shù)據(jù)類型?

                I know that the RHS of both these examples are evaluated as an int. But why doesn't Java convert it to a byte implicitly like it did in the first two cases.Does implicit conversion to smaller data type happen only if there are literals?

                推薦答案

                說明

                讓我們看看你的代碼和一些修改過的例子:

                Explanation

                Lets take a look at your code and some modified examples:

                // Example 1
                byte byteValue = 2;
                
                // Example 2
                byte byteValue = 4/2;
                
                // Example 3
                byte byteValue = 2000;
                
                // Example 4
                byte byteValue = 500/2;
                
                // Example 5
                int n1 = 4;
                byte byteValue = n1/2;
                


                無損轉(zhuǎn)換

                示例 3示例 4示例 5 會出現(xiàn)上述編譯時錯誤.


                Non-lossy conversion

                You will get the mentioned compile-time error for Example 3, Example 4 and Example 5.

                首先,示例 1 到 4 的簡單數(shù)學(xué)運(yùn)算是在編譯時執(zhí)行的.因此 Java 將在編譯時計算 500/2 并將代碼替換為基本上 byte byteValue = 250;.

                First of all, the simple math you have for Example 1 to 4 is executed at compile-time. So Java will compute 500 / 2 at compile-time and replace the code with basically byte byteValue = 250;.

                Java 中字節(jié)的有效值為 -128127.因此,任何超出該范圍的值都不能僅被視為 byte,而是需要顯式轉(zhuǎn)換.因此,示例 1示例 2 通過.

                Valid values for bytes in Java are -128 to 127. So any value outside of that range can not just be taken as a byte but requires explicit conversion. Because of that, Example 1 and Example 2 pass.

                要了解其余部分失敗的原因,我們必須研究 Java 語言規(guī)范 (JLS),更具體的章節(jié) 5.1.3.縮小原始轉(zhuǎn)換范圍和5.2.分配上下文.

                To understand why the rest fails, we have to study the Java Language Specification (JLS), more specifically chapter 5.1.3. Narrowing Primitive Conversion and 5.2. Assignment Contexts.

                它表示從 intbyte 的轉(zhuǎn)換(如果它超出 byte 的范圍)是一個縮小原始轉(zhuǎn)換,并且它可能會丟失信息(原因很明顯).它繼續(xù)解釋轉(zhuǎn)換是如何完成的:

                It says that a conversion from int to byte (if it is outside of the range of byte) is a narrowing primitive conversion and that it may lose information (for obvious reasons). It continues by explaining how the conversion is done:

                有符號整數(shù)到整數(shù)類型 T 的窄化轉(zhuǎn)換只會丟棄除 n 個最低位之外的所有位,其中 n 是用于表示類型 T 的位數(shù).除了可能丟失有關(guān)數(shù)值,這可能會導(dǎo)致結(jié)果值的符號與輸入值的符號不同.

                A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

                從第二章開始,如果值為常量表達(dá)式,則允許進(jìn)行窄轉(zhuǎn)換的賦值.

                From the second chapter, assignments with narrow conversions are allowed if the value is a constant expression.

                此外,如果表達(dá)式是 byte、short、char 或 int 類型的常量表達(dá)式(第 15.29 節(jié)):

                In addition, if the expression is a constant expression (§15.29) of type byte, short, char, or int:

                如果變量是 byte、short 或 char 類型,并且常量表達(dá)式的值可以在變量的類型中表示,則可以使用縮小原語轉(zhuǎn)換.

                A narrowing primitive conversion may be used if the variable is of type byte, short, or char, and the value of the constant expression is representable in the type of the variable.

                長話短說,可能會丟失信息(因為值超出范圍)的縮小轉(zhuǎn)換必須明確地通知 Java.Java 不會在您不強(qiáng)制的情況下為您完成它.這是由演員完成的.

                Long story short, a narrowing conversion that may lose information (because the value exceeds the range) has to explicitly be announced to Java. Java will not just do it for you without you forcing it. That is done by a cast.

                例如

                byte byteValue = (byte) (500 / 2);
                

                導(dǎo)致值 -6.

                你的最后一個例子很有趣:

                Your last example is very interesting:

                int n1 = 4;
                byte byteValue = n1/2;
                

                雖然這沒有超出范圍,但Java仍然將其視為有損縮小轉(zhuǎn)換.為什么會這樣?

                Although this does not exceed the range, Java still treats it as lossy narrowing conversion. Why is that the case?

                好吧,Java 不能 100% 保證 n1 在執(zhí)行 n1/2 之前的最后一秒沒有改變.因此,它必須考慮您的所有代碼,以查看是否有人偷偷訪問 n1 并對其進(jìn)行更改.Java 不會在編譯時進(jìn)行這種分析.

                Well, Java can not ensure 100% that n1 is not changed last second before n1/2 is executed. Therefore, it would have to consider all of your code to see if maybe someone accesses n1 sneaky and changes it. Java does not do this kind of analysis at compile-time.

                因此,如果您可以告訴Java n1 保持4 并且實際上永遠(yuǎn)不會改變,那么這將實際編譯.在這種特定情況下,將其設(shè)為 final 就足夠了.所以與

                So if you can tell Java that n1 stays 4 and can actually never change, then this will actually compile. In this specific case, it would be enough to make it final. So with

                final int n1 = 4;
                byte byteValue = n1/2;
                

                它實際上會編譯,因為 Java 知道 n1 保持 4 并且不能再更改.因此它可以在編譯時將 n1/2 計算為 2 并將代碼替換為基本上 byte byteValue = 2;,它在范圍內(nèi).

                it will actually compile because Java knows that n1 stays 4 and can not change anymore. Hence it can compute n1/2 at compile-time to 2 and replace the code with basically byte byteValue = 2;, which is in range.

                所以你把 n1/2 做成了一個常量表達(dá)式,正如之前在 5.2.分配上下文.

                So you made n1 / 2 a constant expression, as explained before in 5.2. Assignment Contexts.

                您可以在 15.29.常量表達(dá)式.基本上所有簡單的東西都可以輕松計算,無需任何方法調(diào)用或其他花哨的東西.

                You can check the details what it needs to have a constant expression in 15.29. Constant Expressions. Basically everything simple that can easily be computed in place without any method invocations or other fancy stuff.

                這篇關(guān)于Java 中的隱式轉(zhuǎn)換是如何工作的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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ù)的三元表達(dá)式的類型是什么?)
                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='QA0S7'></tbody>
                • <bdo id='QA0S7'></bdo><ul id='QA0S7'></ul>
                • <small id='QA0S7'></small><noframes id='QA0S7'>

                • <legend id='QA0S7'><style id='QA0S7'><dir id='QA0S7'><q id='QA0S7'></q></dir></style></legend>

                      <tfoot id='QA0S7'></tfoot>

                          <i id='QA0S7'><tr id='QA0S7'><dt id='QA0S7'><q id='QA0S7'><span id='QA0S7'><b id='QA0S7'><form id='QA0S7'><ins id='QA0S7'></ins><ul id='QA0S7'></ul><sub id='QA0S7'></sub></form><legend id='QA0S7'></legend><bdo id='QA0S7'><pre id='QA0S7'><center id='QA0S7'></center></pre></bdo></b><th id='QA0S7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='QA0S7'><tfoot id='QA0S7'></tfoot><dl id='QA0S7'><fieldset id='QA0S7'></fieldset></dl></div>
                          主站蜘蛛池模板: 亚洲免费在线 | 中文字幕av网 | 日日骚网| 一区二区在线视频 | 狠狠爱综合 | 欧美日韩精选 | 日韩在线精品视频 | 国产精品18久久久久久白浆动漫 | 日韩av在线一区 | 亚洲码欧美码一区二区三区 | 九九久久久 | 欧美亚洲国语精品一区二区 | 亚洲欧美中文字幕在线观看 | 亚洲精品成人 | 日韩欧美在线观看一区 | 在线视频91 | 久久com | 国产亚洲精品久久久久动 | 国产乱码高清区二区三区在线 | 中文字幕在线一区二区三区 | 免费成年网站 | 成人免费共享视频 | 国产aⅴ精品 | www.黄色在线观看 | 国产精品99久久久久久久vr | 精品久久久久久久久久 | 国产一区二区精品在线观看 | 国产在线成人 | 亚洲精品视频一区二区三区 | 欧美人妇做爰xxxⅹ性高电影 | 蜜桃传媒av | 国产成人精品午夜视频免费 | 综合激情久久 | 欧美日韩视频在线第一区 | 中文字幕在线观看日韩 | 欧美日韩中文字幕在线 | 一级片片 | 美女国产| 日韩一区二区福利视频 | 国产精品久久久久久婷婷天堂 | 国产欧美日韩一区二区三区在线 |