所有整型包装类对象之间值的比较,全部使用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[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
// 省略其它......
}
Integer 源码
public final class Integer extends Number implements Comparable<Integer> {
/**
*最小值 -2^31
*/
@Native public static final int MIN_VALUE = 0x80000000;
/**
* 最大值 2^31 - 1
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
/**
* The {@code Class} instance representing the primitive type
* {@code int}.
*
* @since JDK1.1
*/
@SuppressWarnings("unchecked")
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
// 省略其它......
/**
/**
* 默认缓存 -128 ~ 127 (包含)的值。
* 该缓存会在第一次使用时初始化。
* 该缓存的最大值可通过{-XX:AutoBoxCacheMax=<size>}配置,最小值不可更改。
* 在VM 初始化时,java.lang.Integer.IntegerCache.high 的值会被设置并保存在the private system properties in the sun.misc.VM class。
*
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
}
Long 源码
public final class Long extends Number implements Comparable<Long> {
/**
* 最小值 -2^63
*/
@Native public static final long MIN_VALUE = 0x8000000000000000L;
/**
* 最大值 2^63 -1
*/
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
/**
* The {@code Class} instance representing the primitive type
* {@code long}.
*
* @since JDK1.1
*/
@SuppressWarnings("unchecked")
public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
// 省略其它......
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
// 省略其它......
}
博客探讨了Java中Short, Integer, Long包装类对象的值比较,建议在-128到127范围外使用equals方法,因为这个范围内可能会复用对象导致误用'=='。"
112754087,10293690,深度时空网络模型:融合扩张卷积与区域转移注意力机制,"['深度学习', '卷积神经网络', '递归神经网络', '交通预测', '注意力机制']
1824

被折叠的 条评论
为什么被折叠?



