装箱和拆箱和ValueOf以及XXXValue -- Java语法糖(Syntax sugar)

自动装箱拆箱发生在基本类型和其包装型互操作的时候。

以前一直不知的拆箱和装箱是valueOf和xxValue的别名。是语法糖的一种

 

 

Java代码   收藏代码
  1. public static void main(String[] args) {  
  2.         Integer i = 10;  
  3.         Integer j = 10;  
  4.         System.out.println(j == i); //(1) true  
  5.   
  6.         i = 10000;  
  7.         j = 10000;  
  8.         System.out.println(j == i); //(2) false  
  9.   
  10.         i = new Integer(10000);  
  11.         System.out.println(i == 10000); //(3) true  
  12.         System.out.println(10000 == i); //(4)  true  
  13.         System.out.println(i == 10000L);//(5) true  
  14.   
  15.     }  

 

 

(1)处的结果是 true。

(2)处的结果是false。

(3)处的结果是true,true,true

 

(1),(2)处的结果不同让人惊讶。i = 10;和i=10000;发生了自动装箱,编译器自动编译成 Integer.valueOf(10)和Integer.valueOf(10000)。看看这个方法的实现。

 

Java代码   收藏代码
  1. public static Integer valueOf(int i) {  
  2.         if(i >= -128 && i <= IntegerCache.high)  
  3.             return IntegerCache.cache[i + 128];  
  4.         else  
  5.             return new Integer(i);  
  6.     }  

 

看到某些Integer对象是有缓存的,范围是-128,到high。

再看看IntegerCachestatic final int high;

Java代码   收藏代码
  1. static final Integer cache[];  
  2.   
  3. static {  
  4.     final int low = -128;  
  5.   
  6.     // high value may be configured by property  
  7.     int h = 127;  
  8.     if (integerCacheHighPropValue != null) {  
  9.         // Use Long.decode here to avoid invoking methods that  
  10.         // require Integer's autoboxing cache to be initialized  
  11.         int i = Long.decode(integerCacheHighPropValue).intValue();  
  12.         i = Math.max(i, 127);  
  13.         // Maximum array size is Integer.MAX_VALUE  
  14.         h = Math.min(i, Integer.MAX_VALUE - -low);  
  15.     }  
  16.     high = h;  
  17.   
  18.     cache = new Integer[(high - low) + 1];  
  19.     int j = low;  
  20.     for(int k = 0; k < cache.length; k++)  
  21.         cache[k] = new Integer(j++);  
  22. }  

    high默认是127,可以通过integerCacheHighPropValue

调整,看看这个属性的说明

 

Java代码   收藏代码
  1. /** 
  2.     * Cache to support the object identity semantics of autoboxing for values between  
  3.     * -128 and 127 (inclusive) as required by JLS. 
  4.     * 
  5.     * The cache is initialized on first usage. During VM initialization the 
  6.     * getAndRemoveCacheProperties method may be used to get and remove any system 
  7.     * properites that configure the cache size. At this time, the size of the 
  8.     * cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>. 
  9.     */  
  10.   
  11.    // value of java.lang.Integer.IntegerCache.high property (obtained during VM init)  
  12.    private static String integerCacheHighPropValue;  

 可以通过该参数调整缓存的范围  -XX:AutoBoxCacheMax=<size>

 

3,4,5处发生拆箱,编译之后其实调用了intValue,或者longValue。获得的是基本类型。

 

 

总结: 两个包装类型的值比较,最好不要用 ==


http://jilen.iteye.com/blog/1189353


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值