integer包装类

 

 

integer包装类

 

Number抽象类中定义了拆箱的方法

integer包装类

那么有哪些包装类继承来Number呢下图所示:

integer包装类

自动装箱则是调用了

integer包装类

 public static Integer valueOf(String s, int radix) throws NumberFormatException {
        return Integer.valueOf(parseInt(s,radix));
    }

 public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

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

 

integer的构造方法

integer包装类

public Integer(int value) {
        this.value = value;
    }

public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
    }

 

总结:有源码可知使用new关键字定义一个integer对象的时候压根没有缓存的概念,所以是两个不同的对象

如果是使用自动装箱定义的integer,则由源码可知有一个-128到+127的缓存区,所以为同一对象

在进行运算的时候,integer类型会自动拆箱,所以有如下规则:

integer包装类

integer包装类

 

integer包装类