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中的基本数据类型...