Java基礎2:基本數據類型與常量池


本節主要介紹基本數據類型的大小,自動拆箱裝箱,基本數據類型的存儲方式,以及常量池的原理。

基本數據類型的大小:

int 32位 4字節  
short 16位
float 32位
double 64位
long 64位
char 16位
byte 8位
boolean 1位
自動拆箱和裝箱的意思就是,計算數值時,integer會自動轉為int進行計算。
而當int傳入類型為integer的引用時,int數值又會被包裝為integer。

  

 1 //8位
 2 byte bx = Byte.MAX_VALUE;
 3 byte bn = Byte.MIN_VALUE;
 4 //16位
 5 short sx = Short.MAX_VALUE;
 6 short sn = Short.MIN_VALUE;
 7 //32位
 8 int ix = Integer.MAX_VALUE;
 9 int in = Integer.MIN_VALUE;
10 //64位
11 long lx = Long.MAX_VALUE;
12 long ln = Long.MIN_VALUE;
13 //32位
14 float fx = Float.MAX_VALUE;
15 float fn = Float.MIN_VALUE;
16 //64位
17 double dx = Double.MAX_VALUE;
18 double dn = Double.MIN_VALUE;
19 //16位
20 char cx = Character.MAX_VALUE;
21 char cn = Character.MIN_VALUE;
22 //1位
23 boolean bt = Boolean.TRUE;
24 boolean bf = Boolean.FALSE;

自動拆箱和裝箱:

 1 //基本數據類型的常量池是-128到127之間。
 2 // 在這個范圍中的基本數據類的包裝類可以自動拆箱,比較時直接比較數值大小。
 3 public static void main(String[] args) {
 4     //int的自動拆箱和裝箱只在-128到127范圍中進行,超過該范圍的兩個integer的 == 判斷是會返回false的。
 5     Integer a1 = 128;
 6     Integer a2 = -128;
 7     Integer a3 = -128;
 8     Integer a4 = 128;
 9     System.out.println(a1 == a4);
10     System.out.println(a2 == a3);
11 
12     Byte b1 = 127;
13     Byte b2 = 127;
14     Byte b3 = -128;
15     Byte b4 = -128;
16     //byte都是相等的,因為范圍就在-128到127之間
17     System.out.println(b1 == b2);
18     System.out.println(b3 == b4);
19 
20     //
21     Long c1 = 128L;
22     Long c2 = 128L;
23     Long c3 = -128L;
24     Long c4 = -128L;
25     System.out.println(c1 == c2);
26     System.out.println(c3 == c4);
27 
28     //char沒有負值
29     //發現char也是在0到127之間自動拆箱
30     Character d1 = 128;
31     Character d2 = 128;
32     Character d3 = 127;
33     Character d4 = 127;
34     System.out.println(d1 == d2);
35     System.out.println(d3 == d4);
36 
37 
38     Integer i = 10;
39     Byte b = 10;
40     //比較Byte和Integer.兩個對象無法直接比較,報錯
41     //System.out.println(i == b);
42     System.out.println("i == b " + i.equals(b));
43     //答案是false,因為包裝類的比較時先比較是否是同一個類,不是的話直接返回false.
44     int ii = 128;
45     short ss = 128;
46     long ll = 128;
47     char cc = 128;
48     System.out.println("ii == bb " + (ii == ss));
49     System.out.println("ii == ll " + (ii == ll));
50     System.out.println("ii == cc " + (ii == cc));
51     //這時候都是true,因為基本數據類型直接比較值,值一樣就可以。

總結:注意基本數據類型的拆箱裝箱,以及對常量池的理解。

基本數據類型的存儲方式:

 1 上面自動拆箱和裝箱的原理其實與常量池有關。
 2 3.1存在棧中:
 3 public void(int a)
 4 {
 5 int i = 1;
 6 int j = 1;
 7 }
 8 方法中的i 存在虛擬機棧的局部變量表里,i是一個引用,j也是一個引用,它們都指向局部變量表里的整型值 1.
 9 int a是傳值引用,所以a也會存在局部變量表。
10 
11 3.2存在堆里:
12 class A{
13 int i = 1;
14 A a = new A();
15 }
16 i是類的成員變量。類實例化的對象存在堆中,所以成員變量也存在堆中,引用a存的是對象的地址,引用i存的是值,這個值1也會存在堆中。可以理解為引用i指向了這個值1。也可以理解為i就是1.
17 
18 3.3包裝類對象怎么存
19 其實我們說的常量池也可以叫對象池。
20 比如String a= new String("a").intern()時會先在常量池找是否有“a"對象如果有的話直接返回“a"對象在常量池的地址,即讓引用a指向常量”a"對象的內存地址。
21 public native String intern();
22 Integer也是同理。

Integer類型在常量池中找同值對象的方法:

 1 public static Integer valueOf(int i) {
 2     if (i >= IntegerCache.low && i <= IntegerCache.high)
 3         return IntegerCache.cache[i + (-IntegerCache.low)];
 4     return new Integer(i);
 5 }
 6 private static class IntegerCache {
 7     static final int low = -128;
 8     static final int high;
 9     static final Integer cache[];
10 
11     static {
12         // high value may be configured by property
13         int h = 127;
14         String integerCacheHighPropValue =
15             sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
16         if (integerCacheHighPropValue != null) {
17             try {
18                 int i = parseInt(integerCacheHighPropValue);
19                 i = Math.max(i, 127);
20                 // Maximum array size is Integer.MAX_VALUE
21                 h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
22             } catch( NumberFormatException nfe) {
23                 // If the property cannot be parsed into an int, ignore it.
24             }
25         }
26         high = h;
27 
28         cache = new Integer[(high - low) + 1];
29         int j = low;
30         for(int k = 0; k < cache.length; k++)
31             cache[k] = new Integer(j++);
32 
33         // range [-128, 127] must be interned (JLS7 5.1.7)
34         assert IntegerCache.high >= 127;
35     }
36 
37     private IntegerCache() {}
38 }

所以基本數據類型的包裝類型可以在常量池查找對應值的對應值的對象,找不到就會自動在常量池創建該值的對象。

而String類型可以通過intern來完成這個操作。

JDK1.7后,常量池被放入到堆空間中,這導致intern()函數的功能不同,下面這段代碼很好的解釋了:

 1 [java] view plain copy
 2 String s = new String("1");  
 3 s.intern();  
 4 String s2 = "1";  
 5 System.out.println(s == s2);  
 6   
 7 String s3 = new String("1") + new String("1");  
 8 s3.intern();  
 9 String s4 = "11";  
10 System.out.println(s3 == s4);  
11 輸出結果為:
12 
13 [java] view plain copy
14 JDK1.6以及以下:false false  
15 JDK1.7以及以上:false true

原因解釋:

JDK1.6查找到常量池存在相同值的對象時會返回該對象的地址。

JDK1.7后,intern方法還是會先去查詢常量池中是否有已經存在,如果存在,則返回常量池中的引用,這一點與之前的沒有什么區別,區別在於,如果常量池找不到對應的字符串,則不會將字符串拷貝到常量池,而只識在常量池中生成一個對原字符串的引用。

那么其他字符串在常量池找值時就會返回另一個堆中的對象的地址。

 

 

 


免責聲明!

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



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