Java基础-包装类型及其缓存池

基本类型都有对应的包装类型,为了解决基本数据类型无法面向对象编程所提供的。
byte-Byte
boolean-Boolean
short-Short
char-Character
int-Integer
long-Long
float-Float
double-Double

装箱与拆箱

Integer x = 2;     // 装箱 调用了 Integer.valueOf(2)
int y = x;         // 拆箱 调用了 X.intValue()

缓存池

new Integer(123) 与 Integer.valueOf(123) 的区别在于:

  • new Integer(123) 每次都会新建一个对象;
  • Integer.valueOf(123) 会使用缓存池中的对象,多次调用会取得同一个对象的引用。
posted @ 2020-08-28 17:08  rikarika  阅读(193)  评论(0)    收藏  举报