java Integer包装类之自动装拆箱 + Integer
java Integer包装类之自装拆箱 + Integer == (缓存池)面试题
Integer 自动装拆箱
放置一个大佬的传送门: https://blog.csdn.net/qq_40807739/article/details/87164018
java代码
/**
* @description:
* @author: black tea
* @date: 2021/7/13 13:33
*/
public class IntegerTest {
public static void main(String[] args) {
int i = 10;
Integer j = 20;
System.out.println(i==j);
}
}
javac 之后编译的class
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.jianeng.iot.system;
public class IntegerTest {
public IntegerTest() {
}
public static void main(String[] var0) {
byte var1 = 10;
Integer var2 = 20;
System.out.println(var1 == var2);
}
}
javap -c 后反编译结果
E:\javaCode\jianengiot\jianengiot-system\src\test\java\com\jianeng\iot\system>javap -c IntegerTest.class
Compiled from "IntegerTest.java"
public class com.jianeng.iot.system.IntegerTest {
public com.jianeng.iot.system.IntegerTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: bipush 10
2: istore_1
3: bipush 20
5: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
8: astore_2
9: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
12: iload_1
13: aload_2
14: invokevirtual #4 // Method java/lang/Integer.intValue:()I
17: if_icmpne 24
20: iconst_1
21: goto 25
24: iconst_0
25: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
28: return
}
会发现输出了一些看不懂的东西,我这里放置一个大佬的传送门(javap指令集): https://blog.csdn.net/u010994966/article/details/97408342
内容解析:
自动装箱
5: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
这里通过比对指令集知道是
后面也有注释描述是调用了 Integer.valueOf,如下图:
image确实是一个静态方法,这里从java 或者是 class中都没有直接的进行编写调用,但是最终的执行却是存在的,所以这里就是Integer的自动装箱。
自动拆箱
14: invokevirtual #4 // Method java/lang/Integer.intValue:()I
这里通过比对指令集知道是
后面也有注释描述是调用了 Integer.intValue:(),如下图:
image确实是对象(Integer)一个方法,这里从java 或者是 class中都没有直接的进行编写调用,但是最终的执行却是存在的,所以这里就是Integer的自动拆箱。
Integer 缓存池导致的 == 问题
== 与 equals的比较
== : 基础类型比较的是 值 ,包装类型引用的是 对象的引用地址
equals :一般为 (引用地址 || 值) 比较,需要在比较的对象中重写Object类的equals方法
Class | == | equals |
---|---|---|
Integer | 基础类型比较的是 值 ,包装类型引用的是 对象的引用地址 | 比较对象的值, image |
String | 比较对象 | 先比较对象的引用地址,后再比较值,满足其一即可, image |
Integer 缓存池
Integer.class 中有一个静态内部类 IntegerCache.class
因为源码太长了,截图不下,我直接放上来,
/**
* 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) { // 通过IntegerCache类上的注释可以的出,我们可以设置jvm启动 参数 -XX:AutoBoxCacheMax=<size> 改变缓存池的大小。
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() {}
}
JDK设计者为他们设计了缓存机制,以免频繁创建经常使用的一些对象。
Integer 的自动装箱,使用了这种缓存机制。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Integer 缓存池面试题
public static void main(String[] args) {
Integer i1 = new Integer(88);
Integer i2 = new Integer(88);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
Integer i5 = 97;
Integer i6 = 97;
System.out.println(i5 == i6);
Integer i3 = 197;
Integer i4 = 197;
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
}
最终结果:
false // == 比较对象的引用地址不同
true // equlase 比较值,两者值相同
true // i5 和 i6 均为自动装箱,上文可知自动装箱方法 valueOf ,值从缓存池值获取,因为缓存池范围 是 -128~127 ,所以 两个97的Integer实际指向相同 ----------------------- 不懂的仔细看Integer的自动装箱方法,里面用到了缓存机制。
false // 同上可知,超出127,new Integer(value) 所以两者指向不同
true // 比较值,两者相同