java中自動裝箱,自動拆箱,容易引起空指針異常。

原文:http://www.importnew.com/14229.html


我們要澄清一些誤解,null既不是對象也不是一種類型,它僅是一種特殊的值,你可以將其賦予任何引用類型,你也可以將null轉化成任何類型,來看下面的代碼:

1
2
3
4
5
6
7
String str = null; // null can be assigned to String
Integer itr = null; // you can assign null to Integer also
Double dbl = null// null can also be assigned to Double
 
String myStr = (String) null; // null can be type cast to String
Integer myItr = (Integer) null; // it can also be type casted to Integer
Double myDbl = (Double) null; // yes it's possible, no error
你可以看到在編譯和運行時期,將null強制轉換成任何引用類型都是可行的,在運行時期都不會拋出空指針異常。

4)null可以賦值給引用變量,你不能將null賦給基本類型變量,例如int、double、float、boolean。如果你那樣做了,編譯器將會報錯,如下所示:

1
2
3
4
5
6
7
int i = null; // type mismatch : cannot convert from null to int
short s = null; //  type mismatch : cannot convert from null to short
byte b = null: // type mismatch : cannot convert from null to byte
double d = null; //type mismatch : cannot convert from null to double
 
Integer itr = null; // this is ok
int j = itr; // this is also ok, but NullPointerException at runtime
正如你看到的那樣,當你直接將null賦值給基本類型,會出現編譯錯誤。但是如果將null賦值給包裝類object,然後將object賦給各自的基本類型,編譯器不會報,但是你將會在運行時期遇到空指針異常。這是Java中的自動拆箱導致的,我們將在下一個要點看到它。

5) 任何含有null值的包裝類在Java拆箱生成基本數據類型時候都會拋出一個空指針異常。一些程序員犯這樣的錯誤,他們認爲自動裝箱會將null轉換成各自基本類型的默認值,例如對於int轉換成0,布爾類型轉換成false,但是那是不正確的,如下面所示:

1
2
Integer iAmNull = null;
int i = iAmNull; // Remember - No Compilation Error
但是當你運行上面的代碼片段的時候,你會在控制檯上看到主線程拋出空指針異常。在使用HashMap和Integer鍵值的時候會發生很多這樣的錯誤。當你運行下面代碼的時候就會出現錯誤。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.HashMap;
import java.util.Map;
 
/**
 * An example of Autoboxing and NullPointerExcpetion
 *
 * @author WINDOWS 8
 */
public class Test {
    public static void main(String args[]) throws InterruptedException {
      Map numberAndCount = new HashMap<>();
      int[] numbers = {3, 5, 7,9, 11, 13, 17, 19, 2, 3, 5, 33, 12, 5};
 
      for(int i : numbers){
         int count = numberAndCount.get(i);
         numberAndCount.put(i, count++); // NullPointerException here
      }      
    }
}

輸出:

1
2
Exception in thread "main" java.lang.NullPointerException
 at Test.main(Test.java:25)

這段代碼看起來非常簡單並且沒有錯誤。你所做的一切是找到一個數字在數組中出現了多少次,這是Java數組中典型的尋找重複的技術。開發者首先得到以前的數值,然後再加一,最後把值放回Map裏。程序員可能會以爲,調用put方法時,自動裝箱會自己處理好將int裝箱成Interger,但是他忘記了當一個數字沒有計數值的時候,HashMap的get()方法將會返回null,而不是0,因爲Integer的默認值是null而不是0。當把null值傳遞給一個int型變量的時候自動裝箱將會返回空指針異常。設想一下,如果這段代碼在一個if嵌套裏,沒有在QA環境下運行,但是你一旦放在生產環境裏,BOOM:-)

6)如果使用了帶有null值的引用類型變量,instanceof操作將會返回false:

1
2
3
4
5
6
7
Integer iAmNull = null;
if(iAmNull instanceof Integer){
   System.out.println("iAmNull is instance of Integer");                            
 
}else{
   System.out.println("iAmNull is NOT an instance of Integer");
}

輸出:

1
i
1
AmNull is NOT an instance of Integer

這是instanceof操作一個很重要的特性,使得對類型強制轉換檢查很有用

7)你可能知道不能調用非靜態方法來使用一個值爲null的引用類型變量。它將會拋出空指針異常,但是你可能不知道,你可以使用靜態方法來使用一個值爲null的引用類型變量。因爲靜態方法使用靜態綁定,不會拋出空指針異常。下面是一個例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Testing {            
   public static void main(String args[]){
      Testing myObject = null;
      myObject.iAmStaticMethod();
      myObject.iAmNonStaticMethod();                            
   }
 
   private static void iAmStaticMethod(){
        System.out.println("I am static method, can be called by null reference");
   }
 
   private void iAmNonStaticMethod(){
       System.out.println("I am NON static method, don't date to call me by null");
   }

輸出:

1
2
3
I am static method, can be called by null reference
Exception in thread "main" java.lang.NullPointerException
               at Testing.main(Testing.java:11)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章