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方法还是会先去查询常量池中是否有已经存在,如果存在,则返回常量池中的引用,这一点与之前的没有什么区别,区别在于,如果常量池找不到对应的字符串,则不会将字符串拷贝到常量池,而只识在常量池中生成一个对原字符串的引用。

那么其他字符串在常量池找值时就会返回另一个堆中的对象的地址。

 

原文地址:https://www.cnblogs.com/bukechuji/p/10954676.html