基础知识
- 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
类,Character
、Byte
、 Short
、 Long
等类型也有类似的缓存,只是除了 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 {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
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));
}
}
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
开新的学习之门**
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!