posts - 3,  comments - 2,  trackbacks - 0

昨晚在review一些新人写的代码的时候,发现了下面的代码:

1    Integer a = Integer.valueOf("var1");
2    Integer b = Integer.valueOf("var2");
3    if(a == b){
4            
5    }

当然,关于==和equals()的区别这个是每个java程序员都清楚的了。
但是这位新人却写出了上面这样的代码,着实让人恼火。
在我的追问下,哥们确实知道这个区别。汗,这不是明知故犯嘛,罪加一等。
“我试了var1,var2同时为1时,a==b为true ”。OK,我清楚了,哥们是一知半解了。

于是我便写了下面这段代码给他:
 1        Integer a = Integer.valueOf(1);
 2        Integer b = Integer.valueOf(1);
 3
 4        System.out.println(a == b);
 5        System.out.println(a.equals(b));
 6        
 7        System.out.println("================================");
 8
 9        Integer c = Integer.valueOf(128);
10        Integer d = Integer.valueOf(128);
11
12        System.out.println(c == d);
13        System.out.println(c.equals(d));
14        
15        System.out.println("================================");
16        
17        Integer e = new Integer(1);
18        Integer f = new Integer(1);
19
20        System.out.println(e == f);
21        System.out.println(e.equals(f));
22        
23        System.out.println("================================");
24        
25        
26        Integer g = new Integer(128);
27        Integer h = new Integer(128);
28
29        System.out.println(g == h);
30        System.out.println(g.equals(h));

他看了结果后,很是不解。
无奈,jdk开源是干吗的?看源码啊:

在JDK中的Integer类中有这么一段代码:
 1    private static class IntegerCache {
 2        static final int high;
 3        static final Integer cache[];
 4
 5        static {
 6            final int low = -128;
 7
 8            // high value may be configured by property
 9            int h = 127;
10            if (integerCacheHighPropValue != null{
11                // Use Long.decode here to avoid invoking methods that
12                // require Integer's autoboxing cache to be initialized
13                int i = Long.decode(integerCacheHighPropValue).intValue();
14                i = Math.max(i, 127);
15                // Maximum array size is Integer.MAX_VALUE
16                h = Math.min(i, Integer.MAX_VALUE - -low);
17            }

18            high = h;
19
20            cache = new Integer[(high - low) + 1];
21            int j = low;
22            for(int k = 0; k < cache.length; k++)
23                cache[k] = new Integer(j++);
24        }

25
26        private IntegerCache() {}
27    }

28
29
30    public static Integer valueOf(int i) {
31        if(i >= -128 && i <= IntegerCache.high)
32            return IntegerCache.cache[i + 128];
33        else
34            return new Integer(i);
35    }

看见上面的代码后,“原来这样的啊!!”。
在Integer内中有块缓存,默认保存-128~127之间的Integer 对象,
当你使用Integer.valueOf()静态方法来生成Integer对象时,
会先判断这个数字的大小,若在-128~127之间的话,将直接使用缓存中的对象;
若不是介于这个范围,将新生成对象。但是这个限于Integer.valueOf()静态方法,
若使用构造函数来生成Integer对象时,情况又是不同的。
posted on 2010-08-31 08:31 风萧萧兮 阅读(1200) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔档案

文章分类

文章档案

新闻档案

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜