1. 程式人生 > >慎用自動拆裝箱

慎用自動拆裝箱

先上程式碼:

public static void main(String[] args) throws InterruptedException {
       Integer z = null;
        printf(z);


    }
    public static void printf(int i)
    {
        System.out.println(i);
    }

會報空指標異常:Exception in thread "main" java.lang.NullPointerException

非常低階的錯誤。Integer自動拆箱調的是Integer.intValue()方法,所以,如果物件本身為空的話,調內部的方法就會拋空指標了

 

 

  1. 表示同一種類的欄位 型別定義一致,例如: 價格欄位 都定義成float 或Float
  2. 多考慮引數為空的場景,儘量用邏輯規避