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 对象需要调用其构造方法:

Integer i = new Integer(5);

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

Integer i = 5;  //装箱
int j = i; //拆箱

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

public static void intTest() {
Integer integer = 2;
int i = integer;
System.out.println(i);
}
//反编译后
public static void intTest() {
Integer integer = Integer.valueOf(2);
int i = integer.intValue();
System.out.println(i);
}

IntegerCache 类:

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

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

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

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

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

Integer i1 = 300;
Integer i2 = 300; System.out.println(i1 == i2); //false

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

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

源码里规定了缓存的范围最小为:-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:

        Boolean b1 = false;
Boolean b2 = false;
Boolean b3 = true;
Boolean b4 = true; System.out.println(b1==b2); //true
System.out.println(b3==b4); //true

Integer 与 Long:

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

"==" 与 equals :

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

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

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

     public boolean equals(Object obj) {
return (this == obj);
}

许多类会重写这个方法:

① Integer 类重载了该方法:

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

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

     public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}

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

另外:

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

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

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. HDU-4115 Eliminate the Conflict 2sat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4115 题意:Alice和Bob玩猜拳游戏,Alice知道Bob每次会出什么,为了游戏公平,Bob对Al ...

  2. Windows Server 2008 R2 配置AD(Active Directory)域控制器 -zhai zi wangluo

    http://files.cnblogs.com/zhongweiv/Windows_Server_2008_R2_%E9%85%8D%E7%BD%AEActive_Directory%E5%9F%9 ...

  3. libpcre.so.1 cannot be found

    安装完Nginx之后,启动报错. [vagrant@localhost sbin]$ sudo ./nginx ./nginx: error while loading shared librarie ...

  4. 【转载】ShowWindow函数

    ShowWindow的API函数是显示窗体,但它在第一次调用和以后的调用是有差别的.第一次调用时,它的输入參数nCmdShow是须要输入WinMain函数里传入来的nCmdShow參数,而不能是其他參 ...

  5. 成员方法的this指针

    1.我们知道成员方法中,有个隐式的this常量指针.考虑,Derived继承的成员方法中this指针的表面类型是什么?子类重写的虚方法中this指针的表面类型是什么? 2.Derived继承的方法,就 ...

  6. SQL Server数据库PIVOT函数的使用详解(一)

    http://database.51cto.com/art/201108/285250.htm SQL Server数据库中,PIVOT在帮助中这样描述滴:可以使用 PIVOT 和UNPIVOT 关系 ...

  7. Lucene教程具体解释

    (建立索引)] )中生成的索引文件的存放地址.详细步骤简单介绍例如以下: 1.创建Directory对象,索引目录 2.创建IndexSearch对象,建立查询(參数是Directory对象) 3.创 ...

  8. [Whole Web] [AngularJS] Localize your AngularJS Application with angular-localization

    It is best to start your application's localization efforts early in development, even if you only s ...

  9. linux上gcc

    查看gcc版本号 rpm -qa | grep gcc gnu的gcc是linux/unix下开发的,不能直接在window下运行.window下有gcc的移植版本.就是楼上说的MinGW和cygwi ...

  10. WWDC2016 Session笔记 – Xcode 8 Auto Layout新特性

    目录 1.Incrementally Adopting Auto Layout 2.Design and Runtime Constraints 3.NSGridView 4.Layout Feedb ...