package 常用类;
import java.lang.Integer.IntegerCache;
/*
* 自动装箱,自动拆箱
*/
public class TestAutoBox {
public static void main(String[] args) {
Integer a=123;//自动装箱:Integer a=Integer.valueOf(234);
int b=a;//自动拆箱int b=a.intValue();
Integer c=null;
//if(c!=null){
// int d=c;//c.intValueOf();
//}
Integer i1=Integer.valueOf(-128);
Integer i2=-128;
System.out.println(i1==i2);//true,在-128到127同一对象
System.out.println(i1.equals(i2));//true,值同
System.out.println("########################");
Integer i3=555;
Integer i4=555;
System.out.println(i3==i4);//不是同一个对象,false
System.out.println(i3.equals(i4));//内容的值一样,true
}
}
//源码中的原因
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
自动装箱,自动拆箱
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 概述: 自动装箱:是把基本类型的值转换为对应的包装类对象, 自动拆箱则相反; 数据类型: Java中的基本数据类型...