一、装箱、拆箱定义


  如果一个int型量被传递到需要一个Integer对象的地方,那么,编译器将在幕后插入一个对Integer构造方法的调用,这就叫做自动装箱。而如果一个Integer对象被放到需要int型量的地方,则编译器将幕后插入一个队intValue方法的调用,这就叫做自动拆箱。

    public static void main(String[] args) {
// 装箱
Integer i1 = Integer.valueOf(1);
// 自动装箱
Integer i2 = 1;// 默认执行valueOf(1);
System.out.println(i1 == i2);// true // 自动拆箱
int i3 = i1.intValue();
int i4 = i2.intValue();
System.out.println(i3 == i4);// true // 超出Integer的缓存范围,不从私有静态内部类IntegerCache的数组cache中获得,通过new返回新对象
Integer i5 = 128;
Integer i6 = -129;
Integer i5_1 = 128;
Integer i6_1 = -129;
System.out.println(i5 == i5_1);// false
System.out.println(i6 == i6_1);// false
}

  所以说,对于-127~127之间的值,Integer对象中存在一个IntegerCache的私有静态内部类,这个内部类有一个Integer类型的静态常量数组,在这个内部类中通过静态方法块,初始化了这个静态常量数组。默认这个数组保存[-127,128)之间的Integer对象。源码如下:

     private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[]; static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
} private IntegerCache() {}
}

  通过下面的源码可以知道,为什么Integer i = 128;与Integer y = 128;,通过==比较的结果为false。如果要赋值的int变量在范围内,则返回数组中的对象给Integer,如果不在,则通过带参构造方法,new一个新的Integer对象。

     public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

二、其它包装类型


  The Java Language Specification, 3rd Edition 写道:

    为了节省内存,对于下列包装对象的两个实例,当它们的基本值相同时,他们总是==:

  • Boolean :全部缓存
  • Byte :全部缓存
  • Character : <=127缓存
  • Short : (-128,127)缓存
  • Long : (-128,127)缓存
  • Float : (没有缓存)
  • Double : (没有缓存)

其中Character的缓存源码:

   private static class CharacterCache {
private CharacterCache(){} static final Character cache[] = new Character[127 + 1]; static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}

其中Float没有缓存,直接返回源码:

   public static Float valueOf(String s) throws NumberFormatException {
return new Float(parseFloat(s));
}

三、用处


  除了包装类提供了额外的方法外,当使用集合框架时,泛型为Object类型,所以如果声明为List<int> list...,则这样是不行的,必须声明为List<Integer> list...。

四、存储的位置


  因为是对象,所以存储在堆中。

Java自动装箱拆箱的更多相关文章

  1. java自动装箱拆箱总结

    对于java1.5引入的自动装箱拆箱,之前只是知道一点点,最近在看一篇博客时发现自己对自动装箱拆箱这个特性了解的太少了,所以今天研究了下这个特性.以下是结合测试代码进行的总结. 测试代码: int a ...

  2. JAVA自动装箱拆箱与常量池

    java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...

  3. Java 自动装箱/拆箱

    自动装箱/拆箱大大方便了基本类型(8个基本类型)数据和它们包装类的使用 自动装箱 : 基本类型自动转为包装类(int >> Integer) 自动拆箱: 包装类自动转为基本类型(Integ ...

  4. 深入理解Java自动装箱拆箱机制

    1.自动装箱与拆箱的定义 装箱就是自动将基本数据类型转换为包装器类型(int-->Integer): 拆箱就是自动将包装器类型转换为基本数据类型(Integer-->int). Java中 ...

  5. 那些年一起踩过的坑 — java 自动装箱拆箱问题

    坑在哪里?   我们都知道Java的八种基本数据类型:int, short, long, double, byte, char, float, boolean   分别有各自对应的包装类型:Integ ...

  6. Java 的自动装箱拆箱

    Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类.以下是基本数据类型对应的包装类: 基本数据类型 包装类 byte(1字节) Byte short(2字节) Short int ...

  7. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?

    参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...

  8. java基础1.5版后新特性 自动装箱拆箱 Date SimpleDateFormat Calendar.getInstance()获得一个日历对象 抽象不要生成对象 get set add System.arrayCopy()用于集合等的扩容

    8种基本数据类型的8种包装类 byte Byte short Short int Integer long Long float Float double Double char Character ...

  9. Java的自动装箱/拆箱

    概述 自JDK1.5开始, 引入了自动装箱/拆箱这一语法糖, 它使程序员的代码变得更加简洁, 不再需要进行显式转换.基本类型与包装类型在某些操作符的作用下, 包装类型调用valueOf()方法将原始类 ...

随机推荐

  1. Visual Studio 常用快捷键 (二)

    想不到上一篇 [Visual Studio 常用快捷键] 受这么多人的欢迎.看来大家对Visual Studio的用法非常感兴趣. 接下来我准备写一个 “Visual Studio使用技巧 ” 一个系 ...

  2. Codeforces Round #337 (Div. 2) A. Pasha and Stick 水题

    A. Pasha and Stick   Pasha has a wooden stick of some positive integer length n. He wants to perform ...

  3. IP TCP HTTP Socket的区别

    网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...

  4. spring_150805_datasource

    实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...

  5. malloc/free 和 new/delete 的区别

    1.malloc在C和C++中的区别 1.1.C中可以将任意的指针赋值给void*类型的变量.也可以将void*类型的值赋值给通常的指针变量. ---------------------------- ...

  6. NoSQL数据库有哪些

    NoSQL太火,冒出太多产品了,保守估计也成百上千了. 互联网公司常用的基本集中在以下几种,每种只举一个比较常见或者应用比较成功的例子吧. 1. In-Memory KV Store : Redis ...

  7. iOS开发--即时通讯常用第三方库

    前言 自毕业到现在,从事iOS即时通讯开发已经1年半之久.主要负责Allure开发,目前已上架,可以在苹果商店搜素Allure.Allure模仿微信的交互和设计效果,已经实现微信的大部分功能. 在这里 ...

  8. BufferedReader方法-----Scanner方法

    import java.io.*; import java.util.Scanner; public class C { public static void main(String []args) ...

  9. iOS:UIView的block函数实现转场动画---单视图

    使用UIView动画函数实现转场动画——单视图 + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration ...

  10. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...