java基础复习 - 自动装箱
发布时间:2020-12-15 07:16:01 所属栏目:Java 来源:网络整理
导读:Integer a = 127 ; // 将整形127装箱对象Integer b = 127 ; // 同上 System.out.print( a ==b ); // true System.out.print(a.equals(b)); true Integer a = 127 实际为Integer a =Integer.valueOf(127); Integer a = 128 ;Integer b = 128 ; System.out.pri
Integer a = 127; // 将整形127装箱对象 Integer b = 127; // 同上 System.out.print( a==b ); // true System.out.print(a.equals(b)); true Integer a = 127 实际为Integer a =Integer.valueOf(127); Integer a = 128; Integer b = 128; System.out.print( a==b ); false System.out.print(a.equals(b)); true 127与128 == 运行结果不同的原因在于,当值为小于一个字节时不会再占用另外一个空间,也就是说第一个代码块中a与b占用的是同一块内存,所以为true,但是第二块代码中则不是 源码:
public static Integer valueOf( int i) {
???? if (i >= - 128 && i <= IntegerCache.high) // 没有设置的话,IngegerCache.high 默认是127
???????? return IntegerCache.cache[i + 128 ];
???? else
???????? return new Integer(i);
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |