读书人

Java迷题:等于还是不等于

发布时间: 2012-09-27 11:11:17 作者: rapoo

Java迷题:等于,还是不等于?
等于还是不等于?

看来看下面的一段代码:

?

???代码片段1

     public final class Integer extends Number implements Comparable<Integer> {    private static class IntegerCache {private IntegerCache(){}static final Integer cache[] = new Integer[-(-128) + 127 + 1];static {    for(int i = 0; i < cache.length; i++)cache[i] = new Integer(i - 128);}    }    public static Integer valueOf(int i) {final int offset = 128;if (i >= -128 && i <= 127) { // must cache     return IntegerCache.cache[i + offset];}        return new Integer(i);    }}


装箱: 自动Integer.valueOf();
Integer jjj = 1;
编译后源码:
Integer jjj = Integer.valueOf(1);


拆箱: 自动对象.intValue();
int jjj = new Integer(2);
编译后源码:
int jjj = (new Integer(2)).intValue();
35 楼 zhangkaitao 2012-02-04 之前看到过类似的帖子 http://sishuok.com/forum/blogPost/list/500.html 36 楼 zhangkaitao 2012-02-04 之前看到过类似的帖子 http://sishuok.com/forum/blogPost/list/500.html 37 楼 kjj 2012-02-04 据说对象相等得用equals,对于int == 足够了, 而 Integer是对象,需要equals ,你懂得!! 38 楼 lantian_123 2012-02-04 期望更多高质量的文章

读书人网 >编程

热点推荐