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

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

      <tfoot id='Xlk3Q'></tfoot>

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

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

        為什么 int num = Integer.getInteger("123") 會拋出

        Why does int num = Integer.getInteger(quot;123quot;) throw NullPointerException?(為什么 int num = Integer.getInteger(123) 會拋出 NullPointerException?)

            <bdo id='4uy3w'></bdo><ul id='4uy3w'></ul>

            <small id='4uy3w'></small><noframes id='4uy3w'>

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

                  <tfoot id='4uy3w'></tfoot>

                1. <legend id='4uy3w'><style id='4uy3w'><dir id='4uy3w'><q id='4uy3w'></q></dir></style></legend>
                  本文介紹了為什么 int num = Integer.getInteger("123") 會拋出 NullPointerException?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  以下代碼拋出NullPointerException:

                  int num = Integer.getInteger("123");
                  

                  我的編譯器是否在 null 上調用 getInteger,因為它是靜態的?這沒有任何意義!

                  Is my compiler invoking getInteger on null since it's static? That doesn't make any sense!

                  發生了什么事?

                  推薦答案

                  大局

                  這里有兩個問題:

                  The Big Picture

                  There are two issues at play here:

                  • Integer getInteger(String) 沒有做你認為它做的事情
                    • 在這種情況下返回 null
                    • Integer getInteger(String) doesn't do what you think it does
                      • It returns null in this case
                      • 由于Integernull,NullPointerException被拋出

                      要將 (String) "123" 解析為 (int) 123,您可以使用例如int Integer.parseInt(String).

                      To parse (String) "123" to (int) 123, you can use e.g. int Integer.parseInt(String).

                      • Java 語言指南/自動裝箱
                      • static int parseInt(String)
                      • 靜態整數 getInteger(String)

                      以下是文檔中關于此方法的作用的內容:

                      Here's what the documentation have to say about what this method does:

                      public static Integer getInteger(String nm):確定指定名稱的系統屬性的整數值.如果沒有指定名稱的屬性,如果指定的名稱為空或null,或者如果該屬性沒有正確的數字格式,則返回null.

                      public static Integer getInteger(String nm): Determines the integer value of the system property with the specified name. If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

                      換句話說,該方法與將 String 解析為 int/Integer 值無關,而是與 System.getProperty 方法.

                      In other words, this method has nothing to do with parsing a String to an int/Integer value, but rather, it has to do with System.getProperty method.

                      誠然,這可能是一個相當大的驚喜.不幸的是,圖書館有這樣的驚喜,但它確實教會了你一個寶貴的教訓:總是查閱文檔以確認方法的作用.

                      Admittedly this can be quite a surprise. It's unfortunate that the library has surprises like this, but it does teach you a valuable lesson: always look up the documentation to confirm what a method does.

                      巧合的是,這個問題的一個變體出現在 謎題歸來:Schlock and Awe (TS-5186),Josh Bloch 和 Neal Gafter 的 2009 JavaOne 技術會議演示文稿.這是最后一張幻燈片:

                      Coincindentally, a variation of this problem was featured in Return of the Puzzlers: Schlock and Awe (TS-5186), Josh Bloch and Neal Gafter's 2009 JavaOne Technical Session presentation. Here's the concluding slide:

                      • 圖書館中潛伏著奇怪而可怕的方法
                        • 有些名字聽起來無傷大雅
                        • 確保調用正確的方法
                        • 閱讀庫文檔
                        • 不要違反最小驚訝原則
                        • 不要違反抽象層次結構
                        • 不要為截然不同的行為使用相似的名稱

                        為了完整起見,還有這些方法類似于Integer.getInteger:

                        For completeness, there are also these methods that are analogous to Integer.getInteger:

                        • Boolean.getBoolean(String)
                        • Long.getLong(String)
                        • 最令人震驚的對最小驚訝原則的違反
                        • Java Base API 中最尷尬/誤導性的方法?

                        當然,另一個問題是 NullPointerException 是如何被拋出的.為了專注于這個問題,我們可以將代碼片段簡化如下:

                        The other issue, of course, is how the NullPointerException gets thrown. To focus on this issue, we can simplify the snippet as follows:

                        Integer someInteger = null;
                        int num = someInteger; // throws NullPointerException!!!
                        

                        這里引用 Effective Java 2nd Edition,Item 49:Prefer primitive types to boxed primitives:

                        Here's a quote from Effective Java 2nd Edition, Item 49: Prefer primitive types to boxed primitives:

                        總而言之,只要您有選擇,就優先使用原語而不是盒裝原語.原始類型更簡單、更快.如果您必須使用盒裝圖元,請小心!自動裝箱減少了使用裝箱原語的冗長,但不會降低危險.當您的程序使用 == 運算符比較兩個裝箱原語時,它會進行身份比較,這幾乎肯定不是您想要的.當您的程序進行涉及裝箱和未裝箱原語的混合類型計算時,它會進行拆箱,而當您的程序進行拆箱時,它可能會拋出 NullPointerException.最后,當您的程序將原始值裝箱時,可能會導致成本高昂且不必要的對象創建.

                        In summary, use primitives in preference to boxed primitive whenever you have the choice. Primitive types are simpler and faster. If you must use boxed primitives, be careful! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the == operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throw NullPointerException. Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations.

                        有些地方你別無選擇,只能使用盒裝圖元,例如泛型,否則您應該認真考慮使用盒裝原語的決定是否合理.

                        There are places where you have no choice but to use boxed primitives, e.g. generics, but otherwise you should seriously consider if a decision to use boxed primitives is justified.

                        • 什么是Java/C# 中的 int 和 Integer 的區別?
                        • 為什么 Java 中的自動裝箱允許我有 3 個可能的布爾值?
                        • 是否保證 new Integer(i) ==我在 Java 中?(是的?。?!)
                        • 在 Java 中比較兩個整數時會自動執行拆箱發生?(不!??!)
                        • Java noob:僅限對象的泛型?(是的,很遺憾)
                        • What is the difference between an int and an Integer in Java/C#?
                        • Why does autoboxing in Java allow me to have 3 possible values for a boolean?
                        • Is it guaranteed that new Integer(i) == i in Java? (YES!!!)
                        • When comparing two Integers in Java does auto-unboxing occur? (NO!!!)
                        • Java noob: generics over objects only? (yes, unfortunately)

                        這篇關于為什么 int num = Integer.getInteger("123") 會拋出 NullPointerException?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數轉換為整數?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
                  <legend id='sw9vR'><style id='sw9vR'><dir id='sw9vR'><q id='sw9vR'></q></dir></style></legend>
                    • <bdo id='sw9vR'></bdo><ul id='sw9vR'></ul>

                        <tbody id='sw9vR'></tbody>

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

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

                          1. <tfoot id='sw9vR'></tfoot>
                            主站蜘蛛池模板: 国产精品99久 | 日日夜夜天天 | 日韩手机在线看片 | 337p日本欧洲亚洲大胆 | 久久尤物免费一区二区三区 | 日韩最新网址 | 亚洲高清在线观看 | www.久久影视 | 精品视频在线一区 | 91视频在线 | 久久精品小视频 | 国产精品久久久久久久久久久久久 | 久久久视 | 国产高清在线 | 国产精品久久久久久妇女6080 | 午夜午夜精品一区二区三区文 | 一级在线视频 | 久久精品亚洲成在人线av网址 | 亚洲精品视频一区 | 国产精品欧美精品 | 久久久噜噜噜久久中文字幕色伊伊 | 一级毛片在线看 | 亚洲免费视频在线观看 | 少妇久久久 | 亚洲福利一区 | 四虎成人av | 欧美性猛片aaaaaaa做受 | 久久成人一区二区三区 | 一区二区视频 | 国产一区二区免费 | 成人免费黄色片 | 欧美一级欧美一级在线播放 | 国产线视频精品免费观看视频 | 中文字幕二区三区 | 国产人成精品一区二区三 | 91免费在线 | 久久成人免费观看 | 91视频免费视频 | 大陆一级毛片免费视频观看 | 在线观看中文视频 | 一区二区三区国产 |