- 浏览: 15474 次
- 性别:
- 来自: 杭州
最新评论
在JDK5.0中新增了自动装箱和拆箱,为开发人员解决了很多开发中的效率问题,下面看看是怎么个流程:
1.
Integer c=1; int d=c; System.out.println(d);
结果为:
引用
1
2.
Integer a=null; int b=a; System.out.println(b);
结果为:
引用
Exception in thread "main" java.lang.NullPointerException
at ArrayListTest.main(ArrayListTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
3.缓存机制:默认情况下-XX:AutoBoxCacheMax设置的大小即为缓存的大小,当未设置时即为默认大小-128到127之间的数据全部装箱缓存,在需要时直接获取即可。
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. During VM initialization the * getAndRemoveCacheProperties method may be used to get and remove any system * properites that configure the cache size. At this time, the size of the * cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>. */ // value of java.lang.Integer.IntegerCache.high property (obtained during VM init) private static String integerCacheHighPropValue; static void getAndRemoveCacheProperties() { if (!sun.misc.VM.isBooted()) { Properties props = System.getProperties(); integerCacheHighPropValue = (String)props.remove("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) System.setProperties(props); // remove from system props } } 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 Integer's 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++) cache[k] = new Integer(j++); } private IntegerCache() {} } /** * Returns a <tt>Integer</tt> instance representing the specified * <tt>int</tt> value. * If a new <tt>Integer</tt> instance is not required, this method * should generally be used in preference to the constructor * {@link #Integer(int)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param i an <code>int</code> value. * @return a <tt>Integer</tt> instance representing <tt>i</tt>. * @since 1.5 */ public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); }
可以通过以下方式验证:
Integer e=1234111; Integer f=1234111; System.out.println(e==f); System.out.println(e.equals(f)); Integer g=100; Integer h=100; System.out.println(g==h); System.out.println(g.equals(h));
结果为:
引用
false
true
true
true
发表评论
-
开发工具备忘
2014-06-19 23:41 3281.谷歌公司的:guava; 2.apache公司的:Co ... -
java.util.date
2014-06-17 21:58 331<div class="iteye-blo ... -
(转) spring 的jar各包作用
2014-06-01 15:03 436转自:http://blog.csdn.net/c ... -
(转)Hibernate 的jar各包作用
2014-06-01 15:02 327转自:http://blog.csdn.net/cailian ... -
(转)CyclicBarrier介绍
2014-05-28 08:17 451转自:http://www.iteye.com/topic/9 ... -
(转)Callable,Runnable比较及用法
2014-05-27 13:53 499转自:http://murielily.blog. ... -
(转)java Future 接口介绍
2014-05-27 13:52 437转自:http://blog.csdn.net/yangyan ... -
8_(JAVA基础300集最全教程 学习)Collectioin各容器实现方式
2014-05-15 23:01 3331.ArrayList : 实现方式(非可序化Object数组 ... -
(转)java关键字Transient
2014-05-15 22:52 396转自:http://horst.sun.blog.163.co ... -
(转)Java使用JDBC连接Oracle_MSSQL实例
2014-05-14 23:20 452转自:http://blog.csdn.net/tungkee ... -
ant入门
2014-05-14 23:07 472这几天忙着看项目中的 ... -
(备忘)java.lang.Class类
2014-05-10 21:48 380转自:http://lavasoft.blog.51cto.c ... -
(备忘)native2ascii转码工具使用
2014-05-10 21:49 331native2ascii简介:native2ascii是sun ... -
(转)java.lang.Object类
2014-05-10 21:49 1625转自:http://lavasoft.blog.51cto.c ... -
(备忘录)xml文件的生成和解析_JDOM_4
2014-05-09 14:45 402import java.io.FileNotFou ... -
(备忘录)xml文件的生成和解析_DOM4J_3
2014-05-09 13:58 318import java.io.File; import ... -
(备忘录)xml文件的生成和解析_SAX_2
2014-05-09 11:51 426一边加载一边解析,速度快,内存占用小,但是如果再次使用时需要再 ... -
(转)J2EE十三个规范
2014-05-09 09:48 374转自:http://www.ibm.com/developer ... -
(备忘录)xml文件的生成和解析_DOM_1
2014-05-09 09:25 685xml的解析一般分为两种方式,一种是SAX事件流的方式,另外一 ... -
7_(JAVA基础300集最全教程 学习)方法重载overload
2014-05-08 22:23 432方法重载指的是同样一个方法的多种实现方式,例如我有个加法计算方 ...
相关推荐
什么是自动装箱和自动拆箱
Java中的自动装箱与拆箱Java开发Java经验技巧共6页.pdf.zip
Java中的自动装箱与拆箱_.docx
自动装箱和拆箱从Java 1.5开始引入,目的是将原始类型值转自动地转换成对应的对象。自动装箱与拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接。 如果你在Java...
代码中,我们可以看到自动装箱和拆箱的例子。在自动装箱中,我们将基本类型 int 赋值给 Integer 对象,而在自动拆箱中,我们将 Double 对象赋值给基本类型 double。这样,我们可以直接使用基本类型和包装类型之间的...
自动装箱与拆箱:自动装箱(Autoboxing)指的是自动地将基本数据类型转换为对应的包装类对象;自动拆箱(Unboxing)则是将包装类对象自动转换为基本数据类型。 常用方法:演示如何使用包装类中的这些常见方法。 null...
自动装箱和拆箱问题是Java中一个老生常谈的问题了,我们来一些看一下装箱和拆箱中的若干问题。本文先讲述装箱和拆箱基本的东西,再来看一下面试笔试中经常遇到的与装箱、拆箱相关的问题。 以下是本文的目录大纲:...
Tedu正则表达式与Object与自动装箱与拆箱。
1. 自动装箱: 2. 自动拆箱: 3. 缓存问题
主要给大家介绍了关于Java中自动装箱、拆箱引起的耗时的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
计算机后端-Java-Java核心基础-第14章 面向对象06 24. 新特性:自动装箱与自动拆箱.avi
《深入理解java虚拟机》自动装箱拆箱实例.doc
本篇文章主要介绍了详解Java 自动装箱与拆箱的实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
基本数据(Primitive)类型的自动装箱(autoboxing)、拆箱(unboxing)是自J2SE 5.0开始提供的功能。java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的(在这种情况下包装成为装箱,解包装称为拆箱)
//这里就是自动的装箱,将3转换成Integer类型并放到集合中自动拆箱包装类自动转为基本类型 Integer >> int面试常见题目:自动拆箱与自动装箱的问
java是个包装特别好的绅士,其实即便编译器悄悄在私底下干了什么龌龊事儿,我们一般来说也不是很容易看的到。 不过这些问题可以通过看字节码来解决。 代码很简单: Integer x = 5; x++; 且看字节码: ...
下面小编就为大家带来一篇浅谈Java自动装箱与拆箱及其陷阱。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Java包装类及自动装箱拆箱,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧