Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类。以下是基本数据类型对应的包装类:

基本数据类型

包装类

byte(1字节)

Byte

short(2字节)

Short

int(4字节)

Integer

long(8字节)

Long

float(4字节)

Float

double(8字节)

Double

char(2字节)

Character

boolean(1/8字节)

Boolean

自动装箱、拆箱:

在 jdk1.5 以前,创建 Integer 对象需要调用其构造方法:

  1. Integer i = new Integer(5);

jdk1.5 具有自动装箱拆箱功能:

  1. Integer i = 5; //装箱
  2. int j = i; //拆箱

其原理是调用了 Integer 的 valueOf(int) 和 Integer 对象的 intValue() 方法:

  1. public static void intTest() {
  2. Integer integer = 2;
  3. int i = integer;
  4. System.out.println(i);
  5. }
  6. //反编译后
  7. public static void intTest() {
  8. Integer integer = Integer.valueOf(2);
  9. int i = integer.intValue();
  10. System.out.println(i);
  11. }

IntegerCache 类:

IntegerCache 是 Integer 类中一个私有的静态类,用于整型对象的缓存。

  1. Integer i1 = 2;
  2. Integer i2 = 2;
  3.  
  4. System.out.println(i1 == i2); //true

该缓存策略仅在自动装箱时适用,也就是使用 new Integer() 的方式构建的 Integer 对象!=:

  1. Integer i1 = new Integer(2);
  2. Integer i2 = new Integer(2);
  3.  
  4. System.out.println(i1 == i2); //false

并且只适用于整数区间 -128 到 +127:

  1. Integer i1 = 300;
  2. Integer i2 = 300;
  3.  
  4. System.out.println(i1 == i2); //false

源码里 Integer 的 valueOf(int) 方法:

  1. public static Integer valueOf(int i) {
  2. if (i >= IntegerCache.low && i <= IntegerCache.high)
  3. return IntegerCache.cache[i + (-IntegerCache.low)];
  4. return new Integer(i);
  5. }
  1. private static class IntegerCache {
  2. static final int low = -128;
  3. static final int high;
  4. static final Integer cache[];
  5.  
  6. static {
  7. // high value may be configured by property
  8. int h = 127;
  9. String integerCacheHighPropValue =
  10. sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  11. if (integerCacheHighPropValue != null) {
  12. try {
  13. int i = parseInt(integerCacheHighPropValue);
  14. i = Math.max(i, 127);
  15. // Maximum array size is Integer.MAX_VALUE
  16. h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  17. } catch( NumberFormatException nfe) {
  18. // If the property cannot be parsed into an int, ignore it.
  19. }
  20. }
  21. high = h;
  22.  
  23. cache = new Integer[(high - low) + 1];
  24. int j = low;
  25. for(int k = 0; k < cache.length; k++)
  26. cache[k] = new Integer(j++);
  27.  
  28. // range [-128, 127] must be interned (JLS7 5.1.7)
  29. assert IntegerCache.high >= 127;
  30. }
  31.  
  32. private IntegerCache() {}
  33. }

源码里规定了缓存的范围最小为:-128 到 +127 ,最大值映射到 java.lang.Integer.IntegerCache.high,可以使用 JVM 的启动参数 -XX:AutoBoxCacheMax=size 设置最大值。

其他包装类:

Byte,Short,Long,Character 也有相应的缓存类;

Byte,Short,Long 有固定范围: -128 到 127。对于 Character, 范围是 0 到 127;

另外,只有 Integer 可以通过参数改变范围。

boolean:

  1. Boolean b1 = false;
  2. Boolean b2 = false;
  3. Boolean b3 = true;
  4. Boolean b4 = true;
  5.  
  6. System.out.println(b1==b2); //true
  7. System.out.println(b3==b4); //true

Integer 与 Long:

  1. Integer a = 1;
  2. Integer b = 2;
  3. Long g = 3L;
  4. Long h = 2L;
  5.  
  6. System.out.println(g==(a+b)); //true
  7. System.out.println(g.equals(a+b)); //false
  8. System.out.println(g.equals(a+h)); //ture

"==" 与 equals :

1,”==“可以用于原始值进行比较,也可以用于对象进行比较,当用于对象与对象之间比较时,比较的不是对象代表的值,而是检查两个对象是否是同一对象,这个比较过程中没有自动装箱发生。

2,进行对象值比较不应该使用”==“,而应该使用对象对应的 equals 方法。

3,equals 方法是 Object 类的一个方法:

  1. public boolean equals(Object obj) {
  2. return (this == obj);
  3. }

许多类会重写这个方法:

① Integer 类重载了该方法:

  1. public boolean equals(Object obj) {
  2. if (obj instanceof Integer) {
  3. return value == ((Integer)obj).intValue();
  4. }
  5. return false;
  6. }
  1. Integer i1 = 300;
  2. Integer i2 = 300;
  3.  
  4. System.out.println(i1 == i2); //false 两个不同的地址引用
  5. System.out.println(i1.equals(i2)); //true 两个相同的值(变为两个 int 之间的比较,所以比较值)

② String 重载了该方法:当两值不 == 时,比较他们的值

  1. public boolean equals(Object anObject) {
  2. if (this == anObject) {
  3. return true;
  4. }
  5. if (anObject instanceof String) {
  6. String anotherString = (String)anObject;
  7. int n = value.length;
  8. if (n == anotherString.value.length) {
  9. char v1[] = value;
  10. char v2[] = anotherString.value;
  11. int i = 0;
  12. while (n-- != 0) {
  13. if (v1[i] != v2[i])
  14. return false;
  15. i++;
  16. }
  17. return true;
  18. }
  19. }
  20. return false;
  21. }

所以,当对象没有重写 equals 时,== 与 equals 是等价的。

另外:

  1. Set<Integer> set = new HashSet<>();
  2. set.add(1);
  3. set.add(2);
  4.  
  5. int[] id = new int[9];

  6. //数组下就不能这么装了...
  7. //int[] ints = set.toArray(new Integer[0]);
  8. Integer[] integers = set.toArray(new Integer[0]);
  9.  
  10. for(int i=0;i<=set.size()-1;i++){
  11. id[i] = set.toArray(new Integer[0])[i];
  12. }

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

  1. Java的自动装箱/拆箱

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

  2. JAVA的自动装箱拆箱

    转自:http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html 蛋呢  的空间 ??什么是自动装箱拆箱 基本数据类型的自动装箱(a ...

  3. 通过源码了解Java的自动装箱拆箱

    什么叫装箱 & 拆箱? 将int基本类型转换为Integer包装类型的过程叫做装箱,反之叫拆箱. 首先看一段代码 public static void main(String[] args) ...

  4. java自动装箱拆箱总结

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

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

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

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

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

  7. 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 ...

  8. Java中的自动装箱拆箱

    Java中的自动装箱拆箱 一.自动装箱与自动拆箱 自动装箱就是将基本数据类型转换为包装类类型,自动拆箱就是将包装类类型转换为基本数据类型. 1 // 自动装箱 2 Integer total = 90 ...

  9. Java 自动装箱/拆箱

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

随机推荐

  1. 从html5标准的正式发布到国内CMS的变革

    10月底万维网联盟(W3C)宣布,经过将近8年的艰辛努力,HTML5标准规范终于最终制定完成并正式发布. W3C的正式批准让人们对HTML5更有信心.“这是一个里程碑,标志着很多人员在长达七年时间内投 ...

  2. Scala学习笔记(二)表达式和函数

    笔记的整理主要针对Scala对比Java的新特性:   1.if表达式 if表达式是有结果返回的. val a= if (5>2) "你好" else 1 a的值为if表达式 ...

  3. HDOJ-ACM1097(JAVA) A hard puzzle

    这道题就是HDOJ的1061的变形: 1061 :求n的n次方的个位数 http://www.cnblogs.com/xiezie/p/5596779.html 1097 :求n的m次方的个位数 因此 ...

  4. cygwin设置中文

    cygwin\home\username\.bashrc # 让ls和dir命令显示中文和颜色 alias ls='ls --show-control-chars --color' alias dir ...

  5. php学习小记2 类与对象

    php类的一些特性: 1. 伪变量$this.$this是一个到主叫对象的引用.取值:该方法所从属的对象,可能是另外的对象(前提,当该方法被静态调用时).$this变量存在于一个类的非静态方法中,在静 ...

  6. 获取datagrid中编辑列combobox的value值与text值

    var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'}); var productname = $(ed ...

  7. PowerShell运行cmd命令

    1.使用.exe扩展名 2.使用 cmd /c "" 3.在 PowerShell v3 中有另一种选择来解决这个问题,只需在命令行的任意位置添加 –% 序列(两个短划线和一个百分 ...

  8. 我的Android开发相关文章

    Pro Android学习笔记: Pro Android学习笔记(一零七):2D动画(2):layout渐变动画 2014.7.25 Pro Android学习笔记(一零六):2D动画(1):fram ...

  9. hibernate笔记加强版

    hibernate 一. hibernate介绍 hibernate事实上就是ormapping框架,此框架的作用就是简单话数据库的操作. hibernate就是将用户提交的代码.參照持久化类配置文件 ...

  10. linux调度器 信息解读

    http://blog.csdn.net/wudongxu/article/category/791519