下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java学习z自动装箱和自动拆箱源码分析-编程开发技术java学习之自动装箱和自动拆箱源码分 析原文出处:等风的草自动装箱(boxing)和自动拆箱(unboxing)首先了解下java的四类八种基本数据类型基本类型占用空间(byte)表示范围包装器类型boolean1/8true|falsebooleanchar2-128127characterbyte1-128127byteshort2-2?152?15-1shortint4-2?312?31-1integerlong8-2?632?63-1longfloat4-3 403e383403e38floatdouble8-1.798e3081
2、.798e308double自动装箱java中所谓的装箱通俗点就是:八种基本数据类型在某些条件下使用时,会口 动变为对应的包装器类型。如下清单1:©testpublic void boxingtest() integer i1 二 17;tnteger i2 = 17;integer i3 = 137;integer i4 = 137;system.out println(il 二二 i2);11 system, out. println(i3 二二 i4);输出:truefalse解释下清单1第11句输出true的原因:当包装器类型进行比较时,i3会调用integer.valueo
3、f自动装箱基本数据类型为包装器 类型。/* returns an ©code integer instanee representing the specified* code int value. tf a new code tnteger) instance is not* required, this method should gencreilly be used in prefercncc to* the constructor link ftlnteger(int), as this method is likely* to yield significantly bet
4、ter space and time performance by* caching frequently requested values.* this method will always cache values in the range -128 to 127,* inclusive, and may cache other values outside of this range.* ©param i an ©code int value.* return an code tnteger) instance representing code i).* since
5、 1. 5*/public static integer valueof(int i) if (i 二 integercache. low && i二 integercache. high) return tntegercache. cache i + (tntegercache. 1 ow); return new integer (i);在 lowhigh(-128127),从源码中可以看出,integer对象自动缓存int值范 如果超出这个范围则会自动装箱为包装类。note:1. integer、short> byte> character、long这几个包装
6、类的valueof方法的实现是类 似的;2. double、float的valueof方法的实现是类似的。3. boolean的valueof方法的实现是个三口运算,形如?return?(b?rae?:?mlse);?v自动拆箱jqv3中所谓的拆箱通俗点就是:八种包装器类型在某些条件卜使用时,会口动 变为对应的基本数据类型。清单2:testpublic void unboxingtest() integer il 二 17; int i2 = 17;int i3 二 137;integer i4 二 137;system, out. println(il = i2);10 system, ou
7、t. println(i3 二二 i4);输出:truetrue解释下清单2第10句输出true的原因:当程序执行到第10句时,i4会调用integer. intvalue方法自动拆箱包装器类 型为基木数据类型。/* returns the value of this code integer as an* code int.*/public int intvalue() return value;从源码可以看出,当包装器类型和基本数据类型进行“二二”比较时,包装器类型 会自动拆箱为基本数据类型。清单3内容如f:testpublic void unboxingtest() integer il
8、 二 17;integer i2 = 17;integer i3 = 137;integer i4 = 137;/ 二二system, out println(il 二二 ;system, out. println(i3 = i4);/ equalssystem, out. println (i 1. equal s (i2);15 system, out. println(i3 equals(i4);输出:t ruefalsetruetrue解释第15句为什么会输出true:因为在integer包装类实现的equals方法中,只要比较的当前对象是integer 实例,那么就会自动拆箱为基本数
9、据类型。从以卜tntcgcr类的equals方法的 源码就可看岀:* compares this object to the specified object. the result is* codc true if and only if the argument is not* code null and is an code integer object that* contains the same ©code int value as this object.* param obj the object to compare wi th.* rcturn codc true
10、 if the objects arc the same;* code false otherwise.*/public boolean equals (object obj) if (obj instanceof integer) return value 二二(integer)obj). intvalueo ;return false;note:1. integer、short、byte> character long 这几个包装类的 intvalue 方法的实现是类 似的;2. double、float的intvalue方法的实现是类似的。3. boolean的boolean va
11、lue方法的实现和intvalue方法的实现也是类似的。装箱拆箱综合淸单:public static void main(string args) integer a = 1; integer b 二 2;integer c = 3;integer d = 3;integer e = 321;integer f 二 321;long g = 3l;long h = 2l;/会自动拆箱(会调用intvalue方法)system, out. print!n (cd);/会口动拆箱后再口动装箱system out printin(e=f);/虽然“二二”比较的是引用的是否是同一对彖,但这里有算术运算
12、,如果该引 用为包装器类型则会导致自动拆箱system, out. printin(c=(a+b);/ equals比较的是引用的对象的内容(值)是否相等,但这里有算术运算,如 果该引用为包装器类型则会导/致自动拆箱,再自动装箱/ a+b触发自动拆箱得到值后,再自动装箱与c比较system, out. printin(c. equals(a+b);/首先a+b触发口动拆箱后值为int型,所以比较的是值是否相等system out println(g=(a+b);首先a+b触发自动拆箱后值为int型,自动装箱后为integer型,然后g为 long 型system, out. printin(g. equals(a+b);/首先a+h触发口动拆箱后值为long型,因为int型的3会口动转型为long 型的g然后自动装箱后为long型,/而g也为long型system, out
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 克孜勒苏柯尔克孜自治州阿合奇县2025-2026学年第二学期三年级语文第四单元测试卷部编版含答案
- 通化市柳河县2025-2026学年第二学期六年级语文第五单元测试卷部编版含答案
- 绵阳市江油市2025-2026学年第二学期五年级语文第四单元测试卷(部编版含答案)
- 临夏回族自治州2025-2026学年第二学期六年级语文第五单元测试卷部编版含答案
- 湘潭市湘乡市2025-2026学年第二学期六年级语文第五单元测试卷部编版含答案
- 百色市西林县2025-2026学年第二学期六年级语文第五单元测试卷部编版含答案
- 齐齐哈尔市昂昂溪区2025-2026学年第二学期六年级语文第五单元测试卷部编版含答案
- 读书月策划方案
- 深度解析(2026)《CBT 3893-1999船用立式行星减速器》
- 深度解析(2026)《CBT 309-2008船用内螺纹青铜截止阀》
- 嫁接枣树承包合同协议
- 部编版四年级语文下册第一次月考检测试卷(含答案)
- 7.1影响深远的人文精神课件 -2024-2025学年统编版道德与法治七年级下册
- 医学第十八章颅脑、椎管、脊髓的外科疾病课件
- 呼衰心衰的护理措施
- 竣工结算审计服务投标方案(2024修订版)(技术方案)
- 铲车安全事故
- 华北理工大学《解析几何》2021-2022学年第一学期期末试卷
- 红色简约风电视剧甄嬛传介绍课件
- 2024年广东省深圳市南山区民政局婚姻登记处招聘9人历年(高频重点复习提升训练)共500题附带答案详解
- 安装工程量清单
评论
0/150
提交评论