浅谈java自动装箱,拆箱
来源:互联网 发布:新手编程书 编辑:程序博客网 时间:2024/06/06 01:34
浅谈java自动装箱,拆箱
java有八种基本类型:boolean,char,byte,short,int,long,float,double
其分别对应的包装类:Boolean,Character,Byte,Integer,Long,Float,Double
在J2SE5.0之前必须将基本类型用其包装类打包为对象才能进行操作,J2SE 5.0
开始支持自动装箱,拆箱
- 例子:
Integer a = 2;//自动装箱Integer b = 2;//自动装箱int c = a;//自动拆箱a = 3;//自动装箱b++;//想拆箱,自增后再装箱
- 编译之后:
Integer a = Integer.valueOf(2);Integer b = Integer.valueOf(2);int c = a.intValue();Integer a = Integer.valueOf(3);Integer b = Integer.valueOf(b.intValue()+1));
java的语法糖确实方便好用,减少了代码量,但是乱用的话就可能会掉进陷阱里
- 例如
Integer a = 1; Integer b = 2; Integer c = 3; Integer d = a + b; Integer e = 127; Integer f = 128; Integer g = e + a; Long h = 3L; System.out.println(c == d); System.out.println(c == (a + b)); System.out.println(c.equals(a + b); System.out.println(f == g); System.out.println(f == (e + a)); System.out.println(h == (a + b)); System.out.println(h.equals(a + b));
- 输出结果
true
true
true
false
true
true
false
我们可以看出
c == d 为true 而 f == g为false
这是由于编译之后:
Integer c = Integer.valueOf(a.intValue() + b.intValue());
Integer d = Integer.valueOf(3);
Integer e = Integer.valueOf(127);
Integer f = Integer.valueOf(e.intValue() + a.intValue());
查看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); }
在自动装箱过程中,如果传入的int值在IntegerCache.low和IntegerCache.high之间(-128~127),那么就会直接返回cache中已经创建的实例,否则就会创建新的实例
这里a+b=3所以c和d是指向同一个Integer对象的,而e+a=128所以e和f是指向不同的实例对象
故而c == d 为true ,f ==g为false,其他的Byte,Short,Long,Char也都一样
但是 f == (e +a )为何为true呢?
这句编译之后:
f.intValue() = e.intValue() + a.intValue();
当包装类的”==”运算在遇到算术运算时会自动拆箱
同理 h == (a +b)编译之后就变成了
h.longValue() == (long)(a.intValue() + b.intValue())
最后一句 h.equals(a + b)编译之后
h.equals(Integer.valueOf(a.intValue() + b.intValue()))
包装类的equals方法是不处理数据类型转换的
应此我们在实际编码中应该尽量避免掉进这些陷阱中
结语:这是小子第一次写博客,经验不足,可能文笔粗糙,布局混乱,望各位大哥大姐多多指点。
- 浅谈java自动装箱,拆箱
- 浅谈JAVA中的自动装箱/拆箱
- java自动装箱、拆箱
- Java 自动装箱 拆箱
- Java 自动装箱,拆箱
- java自动装箱、拆箱
- java自动装箱、拆箱
- Java自动装箱/拆箱
- JAVA中的自动装箱/拆箱
- Java的自动装箱,拆箱
- 【java基础】自动装箱、拆箱
- Java自动装箱/拆箱 是什么意思
- java---自动装箱和自动取消装箱
- Java 性能笔记:自动装箱/拆箱(转)
- Java 性能笔记:自动装箱/拆箱
- java自动装箱
- java 自动装箱
- Java自动装箱
- C++上机实验1-2
- bzoj4361 isn
- 测试机器大小端的方法
- [LeetCode]326. Power of Three
- 第一次作业3-1
- 浅谈java自动装箱,拆箱
- 每周学一点Egret(11) Egret Wing 3.0.1插件
- lua笔记
- JavaScript中&&和||的运算
- selenium操作选择列表
- 第一次上机实验—3
- Network学习4_CentOS系统下ifcfg-Auto_em1的正确配置方式
- 就拿胖子说事
- 车辆云诊断构思