所有整型包装类对象之间值的比较,全部使用equals方法比较。
对于 Integer var = ? 在-128 至 127 范围内的赋值,Integer 对象是在 IntegerCache.cache 产 生,会复用已有对象,这个区间内的 Integer 值可以直接使用==进行判断,但是这个区间之外的所有数据,都会在堆上产生,并不会复用已有对象,这是一个大坑,推荐使用 equals 方法进行判断。
Short 源码
public final class Short extends Number implements Comparable<Short> {
/**
* 最小值,-2^15
*/
public static final short MIN_VALUE = -32768;
/**
* 最大值,2^15-1
*/
public static final short MAX_VALUE = 32767;
/**
* short 基本对象
*/
@SuppressWarnings("unchecked")
public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short");
// 省略其它......
/**
* 缓存
**/
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-