8种基本类型:char、byte、short、int、long、float、double、boolean。
对应各自的封装类型。

以 Integer 类为例,其他类型原理类似。
先看比较对象的 equals 方法的重写:

public boolean equals(Object obj) {
        // 首先判断两个对象是不是同一类的对象
        if (obj instanceof Integer) {
            // 然后判断值是否相等
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

所以类型如果不一样,那么直接返回 false。

equals 方法重写后,hashCode 也进行了重写:
以 Integer 为例,hashCode 返回结果就是基本类型的 int 值。

@Override
    public int hashCode() {
        return Integer.hashCode(value);
    }
    public static int hashCode(int value) {
        return value;
    }

下面开始看自动装箱和拆箱的原理。

直接赋值时,都会自动调用 valueOf 方法。

Integer 类中定义了 IntegerCache,这个类中有 Integer 数组,该数组已经缓存了 -128~127 之间的所有 Integer 对象。

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

所以在 valueOf 方法中,将 int 装箱时,判断如果在这个范围中,直接返回对象,而不需要 new 这个新对象。

public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

然后看下边的示例:

public static void main(String[] args) {
        Integer a1 = 12;
        Integer a2 = 12;
        Integer b1 = 200;
        Integer b2 = 200;
        System.out.println(a1==a2); // true
        System.out.println(b1==b2); // false
    }

直接赋值时,都会自动调用 valueOf 方法,而 12 是在 -128~127 之间的,所以缓存中有其实例,两个引用同一个对象。而 200 超出了这个范围,两个对象都是 new 的新对象,两个对象不是同一个。

自动拆箱有个很典型的用法就是在进行运算的时候,因为对象时不能直接进行运算的,而是要转化为基本数据类型后才能进行加减乘除。
看下边的示例:

public static void main(String[] args) {
        Integer a = 1;
        Integer b = 2;
        Integer c = 3;
        Long g = 3L;
        Long h = 2L;

        System.out.println(c == (a + b)); // true
        // 原因:运算要拆箱,a,b,c都拆箱为int,然后运算,a+b为int,c也拆箱为int,基本类型值相等

        System.out.println(c.equals(a + b)); // true
        // 原因:运算要拆箱,a,b拆箱为int之后相加,equals要比较对象,(a+b)需要再装箱为对象
        // 3在-128~127的范围内,两者是同一个对象,对象相同

        System.out.println(g == (a + b)); // true 
        // 原因:运算要拆箱,a,b拆箱为int,g拆箱为long,两边都是基本类型,值相等

        System.out.println(g.equals(a + b)); // false
        // 原因:运算要拆箱,a,b拆箱为int之后相加,equals要比较对象,(a+b)需要再装箱为对象Integer
        // 而g是Long类型的对象,由于不是同一个类的对象,返回false

        System.out.println(g.equals(a + h)); // true
        // 原因:运算要拆箱,a拆箱为int,h拆箱为long,之后相加基本类型会自动向上转型为long
        // 然后equals要比较对象,(a+h)需要再装箱为对象Long
        // 而g也是Long类型的对象,LongCache中也缓存有-128~127的Long对象,所以两者是同一个对象
        // (显然,假如不是3,而是超过范围的某个值,两个对象就不是同一个对象了)
    }

注意
Character 类:缓存 0~127 的字符,其它需要新创建。
Byte,Short,Integer,Long 类:缓存 -128~127 的对象,其它值需要重新创建。
Float,Double 类:没有缓存,直接创建。
Boolean 类:缓存 true 和 false 两个对象,直接赋值即可。