当前位置: 首页 > news >正文

高考写作网站泰州网站制作专业

高考写作网站,泰州网站制作专业,edm营销网站,正规的食品行业网站开发java中的8种基本数据类型#xff1a;boolean byte char short int float double long自动拆装箱的问题引入#xff1a;由于在一开始学习java的时候#xff0c;”万物皆对象“这种面向对象的看问题方式#xff0c;时刻围绕在脑海中。因为静态的变量和基本数据类型不属于对象…java中的8种基本数据类型boolean byte char short int float double long自动拆装箱的问题引入由于在一开始学习java的时候”万物皆对象“这种面向对象的看问题方式时刻围绕在脑海中。因为静态的变量和基本数据类型不属于对象但是由8种基本数据类型的自动装拆箱解决了基本数据类型不是对象。在jdk1.5中引入了自动拆装箱的新特性在jdk1.5之前我们想要使用integer类中的方法我们先要把int变量变成integer类型可以通过new Integer(intNumber) 或者调用Integer.valueOf(intNumber)方法自动装拆箱何时发生1、在调用方法时把基本数据类型作为参数但是参数的类型是基本数据类型的包装类时。在学习过javaSe基础之后我们知道通过使用集合框架ArrayList或者Map来添加元素的时候添加的是Object对象在这里引入ArrayList.add()的源码/*** Appends the specified element to the end of this list.** param e element to be appended to this list* return true (as specified by {link Collection#add})*/public boolean add(E e) {ensureCapacity(size 1); // Increments modCount!!elementData[size] e;return true;}1234567891011对于源码的解读首先我们来看看参数中的(E e)为什么是E 而不是Object因为E代表的是元素(集合中存储的是元素)我们平时在泛型中可以看到 T 代表的是Type 类再比如键值对中的MapK 表示的是KeyV 表示的是Value。E K V 等泛型在使用该参数之前就已经限定好了类型如果赋值给Object的话就不用再进行强制类型转换了。首先把数组的长度1这个操作会导致modCount加一这个变量的作用就是记录当前数组被操作的次数然后直接把参数传进来的对象赋值给上一次长度位置的元素。返回true表示添加成功当我们调用ArrayList.add()方法的时候可以直接调用ArrayList arrayList new ArrayList();arrayList.add(10);12我们反编译这段代码public class AutoboxingTest{public AutoboxingTest(){}public static void main(String args[]){ArrayList arrayList new ArrayList();arrayList.add(Integer.valueOf(100));}}12345678910111213可以看到编译器在编译的时候检测到arrayList.add()需要的是Integer对象所以把int类型自动装箱成Integer类型。2、 给基本数据类型的包装类赋值为基本数据类型的时候。我们还是以Integer和int类型的变量作为例子public class AutoboxingTest2 {public static void main(String[] args) {Integer integer 10;}}1234567继续反编译例子public class AutoboxingTest2{public AutoboxingTest2(){}public static void main(String args[]){Integer integer Integer.valueOf(10);}}123456789101112什么时候自动装箱不起作用当我们要调用的方法中存在重载的时候即基本类型数据作为唯一参数的方法与该基本类型包装类作为唯一参数的方法重载这时候自动装箱不起作用。例子public class InvalidateAutoboxing {public void print(int num) {System.out.println(i am int !);}public void print(Integer num) {System.out.println(i am integer!);}}public class InvalidateAutoboxingTest {public static void main(String[] args) {InvalidateAutoboxing invalidateAutoboxing new InvalidateAutoboxing();invalidateAutoboxing.print(5);}}123456789101112131415161718192021运行结果这里写图片描述使用自动装箱拆箱需要注意的地方1、循环与自动装箱拆箱public class CirculateAndAutoboxingAndAutounboxing {public static void main(String[] args) {Integer sum 0;for (int i 0; i 200; i) {sum i;}}}1234567891011121314反编译public class CirculateAndAutoboxingAndAutounboxing{public CirculateAndAutoboxingAndAutounboxing(){}public static void main(String args[]){Integer sum Integer.valueOf(0);for(int i 0; i 200; i)sum Integer.valueOf(sum.intValue() i);}}1234567891011121314151617反编译代码解读由于sum是Integer类型但是sumi 需要sum先自动拆箱成int类型(调用intValue()方法)进行相加之后再自动装箱成Integer类型把结果赋给sum。Integer.valueOf源码解析/*** Returns a Integer instance representing the specified* int value.* If a new Integer instance is not required, this method* should generally be used in preference to the constructor* Integer(int), as this method is likely to yield* significantly better space and time performance by caching* frequently requested values.** param i an int value.* return a Integer instance representing i.* since 1.5*/public static Integer valueOf(int i) {final int offset 128;if (i -128 i 127) { // must cachereturn IntegerCache.cache[i offset];}return new Integer(i);}1234567891011121314151617181920首先我们在源码说明中看到了该构造器缓存经常使用的数据以减少内存的使用和提高效率Integer把缓冲区的上限脚标设置成128如果传进来的数据i在-128~127之中直接返回缓冲区中的IntegerCache.cache[i 128] 位中的元素IntegerCache的源码解读private static class IntegerCache {private IntegerCache(){}static final Integer cache[] new Integer[-(-128) 127 1];static {for(int i 0; i cache.length; i)cache[i] new Integer(i - 128);}}12345678910IntegerCache是Integer的内部类并且内部数组的上限为256元素在这个内部类中使用了静态代码块(在类加载的时只执行一次)把-128~127都缓存在数组中。所以以后调用的时候就可以直接返回Integer对象而不用return new Integer(i); Integer Short Long的缓冲数组是一样的但是Character的范围为0~127Double和Float没有缓冲数组话又说回来刚刚我们在分析循环与自动装拆箱的使用需要注意当参与的数值不在缓存的范围内会产生大量的对象这样会产生很多垃圾对象增加GC的工作压力。2、自动装拆箱与三元运算符造成的空指针异常(NPE)public class AutounboxingWithConditionalOperator {public static void main(String[] args) {HashMap hashMap new HashMap();Boolean b (hashMap ! null ? hashMap.get(test) : false);}}12345678910运行这里写图片描述发生了空指针异常如果在Map中添加了test这条数据public class AutounboxingWithConditionalOperator {public static void main(String[] args) {HashMap hashMap new HashMap();hashMap.put(test, true);Boolean b (hashMap ! null ? hashMap.get(test) : false);System.out.println(b);}}12345678910运行这里写图片描述我们再反编译刚刚NPE异常的代码public class AutounboxingWithConditionalOperator{public AutounboxingWithConditionalOperator(){}public static void main(String args[]){HashMap hashMap new HashMap();Boolean b Boolean.valueOf(hashMap null ? false : ((Boolean)hashMap.get(test)).booleanValue());}}12345678910111213下面是hashMap.get(key)方法的说明Returns the value to which the specified key is mapped,*or null if this map contains no mapping for the key.由上面可以看出由于Map.get(key)方法如果没有指定的数据返回的是null。再由于三元运算符有如下定义The type of a conditional expression is determined as follows:1、If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.2、If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.3、If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.译文三元运算符的类型由以下情况决定1、如果第二和第三个操作结果拥有同样类型(这个类型可能为null)那么这个类型就是该三元运算符的结果类型2、如果第二个操作和第三个操作结果其中一个的结果类型为基本数据类型另外一个操作结果为可以装箱转换成与前面一种类型一致的包装类那么这个三元运算符的运算结果类型为基本数据类型即把包装类拆装。3、如果在第二、三个操作结果中有一个为null类型另外一个为引用类型那么这个三元运算符的表达式为引用类型。综上所述对于上面出现的情况对于一个操作结果为基本类型另外一个为包装类型的三元运算符表达式来说为了避免NEP的产生可以通过把它们变成同样类型(参考三元运算符定义的第一条)。参考资料Conditional Operator ? :Boxing ConversionThe Java™ Tutorials about Autoboxing and UnboxingWhat is Autoboxing and Unboxing in JavaJava中的自动装箱与拆箱要裝箱還是拆封4.2 自動裝箱、拆箱Java 自动装箱和拆箱Java泛型中K T V E object等的含义自动拆箱导致空指针异常
http://www.zqtcl.cn/news/53336/

相关文章:

  • 肃州区住房和城乡建设局网站企业滴滴app下载
  • 南屏网站建设苏州刚刚发生的大事
  • 网站开发 数据库自助网站建设
  • 做网站销售说辞长沙广告传媒有限公司
  • 郑州网站建设外包互联网营销的概念
  • 商城网站后台模板和平网站建设
  • 网站建设合优做校园二手交易网站的目的
  • 网站建设骗子公司崇州市微信端网站建
  • 南宁彩票网站开发工业软件开发技术
  • 吉安网站建设优化服务flash网站建设价格
  • 新手建设什么网站好php商城网站开发实例视频
  • 建站的网站做同步网站
  • 网站备案是在哪里的学校网站建设及管理制度
  • 红色企业网站wordpress ping optimizer
  • 做门头上那个网站申报牛商网网站做seo好么
  • 做网站建设有前景吗岚山建设网站
  • 莱芜网站建设资情况介绍南开做网站
  • 网站后台管理系统管理员登录施工员证查询官方网站
  • 企业类网站公司推广网站怎么做
  • 做网站主流用什么语言多人在线网站开发
  • 有网站加金币的做弊器吗6免费找客户网站
  • 哪里有免费招聘网站动漫网站在线免费观看
  • 微信有网站开发吗国外设计导航网站大全
  • 手机音乐网站程序源码百度怎么验证网站
  • dedecms win8风格网站模板桂林住房城乡建设厅网站
  • 揭阳模板建站开发公司培训师资格证怎么考
  • 网站的模块无锡网站的优化哪家好
  • 鹤壁做网站哪家好网站开发有哪些语言
  • 做游戏ppt下载网站有哪些绵阳网站建设成都网站设计
  • 如何安装网站模版php网站开发编程软件