可能一眼看不懂的几行Kotlin代码(2)

int a = 100;
Integer aBox = a;
Integer aAnotherBox = a;

int b = 10000;
Integer bBox = b;
Integer bAnotherBox = b;

System.out.println(aBox == aAnotherBox); // true
System.out.println(bBox == bAnotherBox); // false

同时我们在末尾再加上一段测试代码,采用直接new的方式构建对象,得到的比较结果为false

int c= 100;
Integer cBox = new Integer©;
Integer cAnotherBox = new Integer©;
System.out.println(cBox == cAnotherBox); // false

原理

基础知识

  • Kotlin中三等号(=) 比较的是两个引用在内存中指向的是不是同一对象(即同一内存空间),双等号() 比较的是值;
  • Kotlin中的非空Number类型对应到JVM平台是基本类型:int,double等等;
  • Kotlin中的可空Number类型对应到JVM平台是封装类型:Integer,Double等等;
  • Java中双等号(==)比较的是两个引用在内存中指向的是不是同一对象(即同一内存空间);
  • Kotlin中三等号等价于Java中的双等号;

字节码分析

val a: Int = 100

L0
LINENUMBER 12 L0
BIPUSH 100
ISTORE 1

val boxedA: Int? = a
val anotherBoxedA: Int? = a

L1
LINENUMBER 13 L1
ILOAD 1
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 2
L2
LINENUMBER 14 L2
ILOAD 1
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 3

从字节码不难看出,非空 Int 型数据,直接使用BIPUSH压栈(取值 -128~127 时,JVM 采用 BIPUSH 指令将常量压栈)。而针对非空Int型变量赋值给可空 Int 型声明,是通过 Integer 类的 public static Integer valueOf(int i) 方法实现。

查看 Integer 类中 public static Integer valueOf(int i) 方法源码:

public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

看到 IntegerCache 的一瞬间感觉一切都清晰了。JDK 从 1.5 版本开始,把 -128~127(high的默认值) 的数字缓存起来了,用于提升性能和节省内存,通过 -XX:AutoBoxCacheMax=<size> 来控制high的取值。所以,当数字在缓存范围内时,通过valueOf()方式拿到的对象引用全部来自于缓存列表,所以对于相同的值,对象引用相同;若是超过缓存范围,则是重新生成的对象,自然也就不相等了。此时回头看开头的两段 Kotlin 代码和我们改写的 Java 代码,就很清晰了。

/**

  • 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=} 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() {}
}

IDEA 配置调整high值

通过配置-Djava.lang.Integer.IntegerCache.high=<size>或者-XX:AutoBoxCacheMax=<size>调整阈值。

配置方式一

配置方式二

延伸

除了 Integer类,CharacterByteShortLong 等类型也有类似的缓存,只是除了 Integer 外,其他类的缓存范围是不可变的。

private static class ByteCache {
private ByteCache(){}

static final Byte cache[] = new Byte[-(-128) + 127 + 1];

static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}

private static class CharacterCache {

总结

最后小编想说:不论以后选择什么方向发展,目前重要的是把Android方面的技术学好,毕竟其实对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上我整理的几十套腾讯、字节跳动,京东,小米,头条、阿里、美团等公司19年的Android面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

技术进阶之路很漫长,一起共勉吧~
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

技术进阶之路很漫长,一起共勉吧~
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值