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包装类

 

 

 

 

 

 

相关文章:

  • 2021-08-07
  • 2021-12-26
  • 2022-12-23
  • 2021-08-16
  • 2021-07-02
  • 2022-12-23
  • 2018-07-30
  • 2021-07-24
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2021-05-24
  • 2022-03-09
  • 2021-07-26
  • 2021-06-18
  • 2022-01-20
相关资源
相似解决方案