Integer 值的问题
Integer缓存问题
- 经典代码片段
public static void main(String[] args) {
Integer a = 200;
Integer b = 200;
System.out.println(a == b); //false
System.out.println(a.equals(b)); //true
}
-
Integer默认的数字缓存区间为 -128~127 (IntegerCache)
-
可以通过修改JVM启动参数修改IntegerCache上限(-XX:AutoBoxCacheMax=250)
-
Integer为什么需要使用缓存区间
- 以免频繁创建不必要的小对象
- Float和Double没有缓存区间
- IntegerCache 使用的 【享元设计模式】