java的基础类 都有自己对应的包装类
基本数据类型 | 包装类 |
---|---|
byte | Byte |
boolean | Boolean |
short | Short |
char | Char |
int | Integer |
long | Long |
float | Float |
double | Double |
public static void main(String[] args) {
Integer value1=100;
Integer value2 = 100;
System.out.println("value1==value2 "+(value1==value2));
Integer value3=300;
Integer value4=300;
System.out.println("value3==value4 "+(value3==value4));
}
输出的结果
value1==value2 true
value3==value4 false
形成这种原因Integer中的缓存机制有关,在java5中Integer的操作 引入新功能来节省内存和提高性能,整型对象通过使用相同的应用对象实现了缓存和重用
反编译出来的代码
0: bipush 100
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 100
8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
11: astore_2
12: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
15: new #4 // class java/lang/StringBuilder
18: dup
19: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V
22: ldc #6 // String value1==value2
24: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
编译出来的字节码 这里的自动装箱 编译出来的就是字节码就是调用了Integer的valueof
java 源码 有关valueof
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
这里是先判断了这个i的值是否在缓存规定的最大,最小之间吗的值,如果的确实在范围内 就直接返回缓存中的值,否则就重新new一个对象 这个 值的范围是-128 到127 之间也就是 如果数值在这个之间 就直接返回缓存中的值,这样使用"==" 就会是相等的,因为 都是 使用的一个句柄,如果 在这个范围之外,例如300这个数值,就会重新new一个对象,那么再使用 “==” 就会出现false的现象。
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in 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() {}
}
从源码以及源码解释来看,这个-128到127这个数值范围是可以改变的,配置java.lang.Integer.IntegerCache.high参数来设置他的值,这个值会和127做一个比较,取一个较大的值。同时在和最大的的int值加上128取最小值,然后取较小的值。 最小的值是无法改变的一直都是-127
这个值没有提供改变接口,从注释来看 是可以通过配置-XX:AutoBoxCacheMax=这个来改变的
所以在比较包装类的时候最好使用equal()
这种缓存行为不仅适用于Integer对象。我们针对所有的整数类型的类都有类似的缓存机制。
有ByteCache用于缓存Byte对象
有ShortCache用于缓存Short对象
有LongCache用于缓存Long对象
有CharacterCache用于缓存Character对象
Byte, Short, Long有固定范围: -128 到 127。对于Character, 范围是 0 到 127。除了Integer以外,这个范围都不能改变。