java自动装箱拆箱总结.doc_第1页
java自动装箱拆箱总结.doc_第2页
java自动装箱拆箱总结.doc_第3页
java自动装箱拆箱总结.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

郑州IT培训首选达内,专注IT培训13年,java,UI,php,Android,IOS,软件测试linux,.net,Unity 3d,会计,网络营销等共16大培训课程,美国上市公司,15万大学生共同的选择!Z QQ:3158895217咨询电话java自动装箱拆箱总结 对于java1.5引入的自动装箱拆箱,之前只是知道一点点,最近在看一篇博客时发现自己对自动装箱拆箱这个特性了解的太少了,所以今天研究了下这个特性。以下是结合测试代码进行的总结。测试代码: int a = 1; Integer b = 1; Integer c = 1; Integer d = 2; Integer e = 3; Integer f = 128; Integer g = 128; Long h = 3L; Double m = 4.0; Double n = 4.0; Float p = 5f; Float q = 5f; System.out.println(a = b : + (a = b); /true System.out.println(b =c : + (b = c); /true System.out.println(e = (c + d) : + (e = (c + d); /true System.out.println(e.equals(c + d) : + (e.equals(c + d); /true System.out.println(h = (c + d) : + (h = (c + d); /true System.out.println(h.equals(c + d) : + (h.equals(c + d); /false System.out.println(f = g : + (f = g); /false System.out.println(m = n : + (m = n); /false System.out.println(p = q : + (p = q); /false System.out.println(m = d * 2 : + (m = d * 2); /true System.out.println(p = (d + e) : + (p = (d + e); /true测试输出结果与说明: 1. a = b : true 当基本类型包装类与基本类型值进行=运算时,包装类会自动拆箱。即比较的是基本类型值。 具体实现上,是调用了IValue()方法实现拆箱。 可以在测试代码处打断点,使用F5快捷键step into至每一步执行方法,会看到调用了IValue()方法实现了拆箱。 2. b = c : true b和c类型均为Integer包装类,故对基本类型进行自动装箱后赋值给b和c。 在进行=运算时不会触发拆箱操作。所以比较的是引用地址,说明b和c是同一个对象。 java中自动装箱调用的是Integer.valueOf()方法实现的,可以像例1一样打断点验证。 为什么b和c 会是同一个对象呢?查看Integer.valueOf()方法实现即可知道,如下: 下面的代码中可以看到,对于-128至127这256个值,直接获取的IntegerCache中的值。 而IntegerCache是Integer中的一个静态内部类, 里面将-128至127(即一个字节所能表示的所有带符号值 -27至27-1)的包装类存在了一个数组中。 对于-128到127之间的数,直接从数组中获取,其他的数则使用new生成。所以此处输出true。 public static Integer valueOf(int i) if(i = -128 & i = IntegerCache.high) return IntegerCache.cachei + 128; else return new Integer(i); private static class IntegerCache static final int high; static final Integer cache; static final int low = -128; / high value may be configured by property int h = 127; if (integerCacheHighPropValue != null) / Use Long.decode here to avoid invoking methods that / require Integers autoboxing cache to be initialized int i = Long.decode(integerCacheHighPropValue).intValue(); i = Math.max(i, 127); / Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - -low); high = h; cache = new Integer(high - low) + 1; int j = low; for(int k = 0; k cache.length; k+) cachek = new Integer(j+); 3. e = (c + d) : true 包装类在执行加减乘除求余等运算时,会触发拆箱操作,故c、d拆箱后相加, 结果为基本类型;然后e与基本类型进行=运算,触发拆箱操作。 4. e.equals(c + d) : true 首先,c + d 拆箱运算得到基本类型值;然后当进行equals运算时, 会触发基本类型值的装箱操作,c + d 的结果会自动装箱为包装类;最后与e进行equals运算。 5. h = (c + d) : true 运算顺序与上面例3中一致,唯一区别是h自动拆箱是调用了Long.longValue()实现。 6. h.equals(c + d) : false 运算顺序与上面例4中一致,为false的原因在于c + d的运算结果自动装箱后类型为Integer, 而h的类型为Long,类型不一样,equals的结果为false。 7. f = g : false 请参考上面例2中的解释。 超出了-128至127的缓存范围,故在valueOf()方法中使用new生成了新对象。 8. m = n : false p = q : false 与上面例子1、2中的结果不同,对于Boolean、Byte、Character、Short、Integer、Long六种基本类型, 对于一个字节以内的值-128到127(Boolean只有true和false)都实现了缓存机制。 不在此范围的数才在对应的valueOf()方法中new出一个新的对象。 但是对于Double和Float类型的浮点数据,在-128到127之间除了256个整数外还有无数的小数, 故java中没有实现Double和Float中一些数的缓存。 所以,对于Double和Float的自动装箱,都是new出新的对象。故此两例均输出false。 10. m = d * 2 : true p = (d + e) : true 请参考例3和例5。反编译后代码:使用java反编译工具,对class字节码文件进行反编译,结果如下所示。从下面的反编译代码,我们可以看到java是如何实现自动装箱、拆箱的。 int a = 1; Integer b = Integer.valueOf(1); Integer c = Integer.valueOf(1); Integer d = Integer.valueOf(2); Integer e = Integer.valueOf(3); Integer f = Integer.valueOf(128); Integer g = Integer.valueOf(128); Long h = Long.valueOf(3L); Double m = Double.valueOf(4.0D); Double n = Double.valueOf(4.0D); Float p = Float.valueOf(5.0F); Float q = Float.valueOf(5.0F); System.out.println(a = b : + (a = Value(); System.out.println(b =c : + (b = c); System.out.println(e = (c + d) : + (Value() = Value() + Value(); System.out.println(e.equals(c + d) : + e.equals(Integer.valueOf(Value() + Value(); System.out.println(h = (c + d) : + (h.longValue() = Value() + Value(); System.out.println(h.equals(c + d) : + h.equals(Integer.valueOf(Value() + Value(); System.out.println(f = g : + (f = g); System.out.println(m = n : + (m = n); Syst

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论