[转载]java.lang.Integer常见问题

1.介绍

关于Integer和int在面试的时候出现的频率很高。而我们所熟知的是Integer是int 的包装类型,int的初始值为0,Integer的初始值为null,这是基本都知道的。至于Integer的自动装箱和拆箱,以及Integer的缓存等小细节需要深入思考。

2.包装类的装箱和拆箱

从基本数据类型到包装类型的过程是装箱、从包装类型到基本数据类型的过程是拆箱。

例子:


   
   
  1. public static void main(String[] args) {
  2. //标准的装箱过程
  3. Integer i =new Integer(127);
  4. /**
  5. * 这个写法相当于Integer i1 =new Integer(127);
  6. * 这个初始化的过程默认也是使用了自动装箱
  7. * 装箱的本意就是将基本数据类型转换成包装类型
  8. * */
  9. Integer i1 =127;
  10. //标准的拆箱过程
  11. int j =i.intValue();
  12. /**
  13. * 这个写法相当于int j1 =i1.intValue();
  14. * 这个初始化的过程默认也是使用了自动拆箱过程
  15. * 拆箱的本意就是将包装类型转换成基本数据类型
  16. * */
  17. int j1 =i1;
  18. }

int(4字节)Integer
byte(1字节)Byte
short(2字节)Short
long(8字节)Long
float(4字节)Float
double(8字节)Double
char(2字节)Character
boolean(未定)Boolean

3.Integer的缓存

Integer定义了一个静态内部类IntegerCache作为缓存

缓存源码:


   
   
  1. private static class IntegerCache {
  2. //常量最小值-128
  3. static final int low = -128;
  4. //常量最大值
  5. static final int high;
  6. //Integer缓存数组
  7. static final Integer cache[];
  8. static {
  9. //初始化h变量为127
  10. int h = 127;
  11. String integerCacheHighPropValue =
  12. sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  13. if (integerCacheHighPropValue != null) {
  14. int i = parseInt(integerCacheHighPropValue);
  15. //说明i最小可以取到127,规定了i的下限,所以i>=127
  16. i = Math.max(i, 127);
  17. //由于i>=127并且Integer.MAX_VALUE - (-low)>128,因此可以得到h>=127
  18. h = Math.min(i, Integer.MAX_VALUE - (-low));
  19. }
  20. high = h;
  21. //初始化Integer缓存数组
  22. cache = new Integer[(high - low) + 1];
  23. //数组中的初始值是-128
  24. int j = low;
  25. //循环为数组赋值,数组0位是-128,最大是127(临界值)
  26. for(int k = 0; k < cache.length; k++)
  27. cache[k] = new Integer(j++);
  28. }
  29. private IntegerCache() {}
  30. }
  31. /**
  32.  * 返回一个表示指定的 int 值的 Integer 实例。
  33.  * 如果不需要新的 Integer 实例,则通常应优先使用该方法,而不是构造方法 Integer(int)
  34.  * 因为该方法有可能通过缓存经常请求的值而显著提高空间和时间性能
  35.  */
  36. public static Integer valueOf(int i) {
  37.     assert IntegerCache.high >= 127;
  38.     //当-128 <=i<=127时,直接从缓存数组中取出值
  39.     if ( i >= IntegerCache.low && i <= IntegerCache.high)
  40.         return IntegerCache.cache[i + (-IntegerCache.low)];
  41.     //否则返回新的Integer对象
  42.     return new Integer(i);
  43. }
而我们使用Integer i=100的时候采取的装箱操作实际上调用了valueof方法,API也提示尽量多使用valueof的方式创建Integer的实例而不是以new Integer的方式。因为valueof方式使用缓存提高空间的利用率和时间性能。

4.经典面试题


   
   
  1. public static void main(String[] args) {
  2. //标准的装箱过程
  3. Integer i =new Integer(100);
  4. Integer i1 =100;
  5. Integer i2 =100;
  6. Integer i3 =128;
  7. Integer i4 =128;
  8. System.out.println("i==i1 :"+(i==i1));
  9. System.out.println("i1==i2 :"+(i1==i2));
  10. System.out.println("i3==i4 :"+(i3==i4));
  11. }
打印结果:


解释:

(1) i == i1  :由于i本身是new Integer的方式单独开辟空间,i1是 -128<=i1<=127,所以i1是从缓存中取出数据的。而缓存的地址和new Integer单独开辟空间对应的地址不同,返回false。

(2) i1 == i2 :i1是 -128<=i1<=127,所以i1是从缓存中取出数据的。i2是-128<=i2<=127,所以i2也是从缓存中取出数据的。所以i1和i2对应的是同一个缓存地址,返回true。

(3)i3 == i4 :i3>127并且i4>127,所以i3和i4分别是new Integer这种单独开辟空间,地址不同,返回false。




[添加链接描述](https://blog.csdn.net/ya_1249463314/article/details/77507245?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-9.base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-9.base)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值