Java包裝類之實體類不要使用基本類型


[color=rgba(0, 0, 0, 0.75)]今天來記錄一下,在項目中因為基本類型,所產生的bug。**U•ェ•*U**

包裝類:8種基本類型的包裝類

應用場景:數據庫建立實體映射多用包裝類

這兩句話是重點:就是建立實體類禁止使用基本數據量類型!!!而用對應的包裝類,

為什么呢,看以下場景。

 

[Java] 純文本查看 復制代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<font style= "color:rgb(77, 77, 77)" ><font face= "&quot" ><font style= "font-size:16px" > /**
  * 8中基本類型的對應包裝類’
  * byte  short  int  long   double  float  boolean  char
  * Byte Short Integer Long Double  Float  Boolean  Character
  * 區別:(舉例int,其余相同)
  * 1、int默認為0,integer默認為null
  * 2、int是java的基本數據類型,integer是int的包裝類
  * 3、integer必須new,int直接使用
  */
 
/**
  * 場景一:
  * 創建對應數據庫的實體類字段
  * 1、創建一個類型(type),對應數據庫的一個字段
  * 2、注意:此存在嚴重問題,基本類型都默認有值。如int 默認為0
  * 3、那在進行數據庫新增的時候,如果不填,則會默認為0。
  * 4、會產生嚴重的bug,應該改為包裝類的引用類型
  */
//錯誤示范
private int type; //代表類型
//正確,設置為integer類型
private Integer typeT;
</font></font></font>

 

所以,多用包裝類進行賦值。
補充:

 

[Java] 純文本查看 復制代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
<font style= "color:rgb(77, 77, 77)" ><font face= "&quot" ><font style= "font-size:16px" > /**
  * 場景二:
  * 自動裝箱And自動拆箱
  */
private void testBox() {
     //原本轉換方式
     int t = 10 ;
     Integer ct = new Integer(t);
     int tt = ct.intValue();
     int i = 10 ;
     //自動裝
     Integer c = i;
     //自動拆
     int ic = c;
}
</font></font></font>

 

筆試題題如下?為什么一個為true,一個為false???

 

[HTML] 純文本查看 復制代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
< font style = "color:rgb(77, 77, 77)" >< font face = "&quot" >< font style = "font-size:16px" >/**
  * 自動裝拆箱
  */
public static void main(String[] args) {
     Integer integer0 = 127;
     Integer integer1 = 127;
     System.out.println(integer0 == integer1);//等於true
     Integer integer2 = 128;
     Integer integer3 = 128;
     System.out.println(integer2 == integer3);//等於false
 
     /** 源碼
      *    public static Integer valueOf(int i) {
      *             if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
      *                 return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
      *             return new Integer(i);
      *         }
      * 通過上我們發現,如果他的int值在最高和最低之間,他直接返回cache內的數據
      * 否則, new Integer(i);
      * 那么最高值:?=high 127 ,最低值:?=low -128,
      * 所以:在-128至127內,他們引用的是緩存內的數據,地址相同,所以為true。超過此則為false
      *
      *     private static class IntegerCache {
      *         static final int low = -128;
      *         static final int high;
      *         static final Integer cache[];
      *
      *         static {
      *             // high value may be configured by property
      *             int h = 127;
      *             String integerCacheHighPropValue =
      *                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
      *             if (integerCacheHighPropValue != null) {
      *                 try {
      *                     int i = parseInt(integerCacheHighPropValue);
      *                     i = Math.max(i, 127);
      *                     // Maximum array size is Integer.MAX_VALUE
      *                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
      *                 } catch( NumberFormatException nfe) {
      *                     // If the property cannot be parsed into an int, ignore it.
      *                 }
      *             }
      *             high = h;
      *
      *             cache = new Integer[(high - low) + 1];
      *             int j = low;
      *             for(int k = 0; k < cache.length ; k++)
      *                 cache[k] = new Integer(j++);
      *
      *             // range [-128, 127] must be interned (JLS7 5.1.7)
      *             assert IntegerCache.high >= 127;
      *         }
      *
      *         private IntegerCache() {}
      *     }
      *
      */
}</ font ></ font ></ font >

 

注解記錄完畢,這些在日常開發中還是很需要我們注意的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM