java 装箱的作用,Java 拆箱与装箱

本文知识点

基本类型与引用类型

== 与 equals() 的区别

equals() 和 hashCode 的关系

装箱与拆箱的原理

一个非常直观的例子说明:int 和 Integer 的区别

基本类型与引用类型

基本类型

引用类型

描述

char

Character

字符,占 2 字节,'\u0000'~'\uFFFF'

byte

Byte

字节型,占 1 字节,-128~127

short

Short

短整型,占 2 字节,-32768~32767,15次方

int

Integer

整型,占 4 字节,0x80000000~0x7fffffff,32次方

long

Long

长整型,占 8 字节,-2(63)~2(63)-1

float

Float

浮点型,占 4 字节

double

Double

双精度型,占 8 字节

boolean

Boolean

布尔型,两个值

对于占用字节大小,上述各引用类型源码中有一个 SIZE 常量可以查看:

public static final int SIZE = 16;

各引用类型的 equals() 和 hashCode() 方法,以 Integer 为例:

Integer

equals()

public boolean equals(Object obj) {

if (obj instanceof Integer) {

return value == ((Integer)obj).intValue();

}

return false;

}

private final int value;

public int intValue() {

return value;

}

public Integer(char value) {

this.value = value;

}

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() {}

}

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);

}

}

hashCode()

@Override

public int hashCode() {

return Integer.hashCode(value);

}

public static int hashCode(int value) {

return value;

}

Character

equals()

public boolean equals(Object obj) {

if (obj instanceof Character) {

return value == ((Character)obj). charValue();

}

return false;

}

private final char value;

public byte charValue() {

return value;

}

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);

}

}

hashCode()

@Override

public int hashCode() {

return Character.hashCode(value);

}

public static int hashCode(byte value) {

return (int)value;

}

总结 equals() 与 hashCode() 实现

引用类型

equals

hashCode

InterCache

Character

value == ((Character)obj).charValue()

(int)value

CharacterCache - 缓存 0~128 的字符

Byte

value == ((Byte)obj).byteValue()

(int)value

ByteCache - 缓存 -128~127 的 byte 值

Short

value == ((Short)obj).shortValue()

(int)value

ShortCache - 缓存 -128~127 的 short 值

Integer

value == ((Integer)obj).intValue()

value

IntegerCache - 缓存 -128~127 的 int 值

Long

value == ((Long)obj).longValue()

(int)(value ^ (value >>> 32))

LongCache - 缓存 -128~127 的 long 值

Float

(obj instanceof Float) && (floatToIntBits(((Float)obj).value) == floatToIntBits(value))

floatToIntBits(value)

/

Double

(obj instanceof Double) && (doubleToLongBits(((Double)obj).value) ==doubleToLongBits(value))

(int)(doubleToLongBits(value) ^ (bits >>> 32))

/

Boolean

value == ((Boolean)obj).booleanValue()

value ? 1231 : 1237

/

备注:

value 字段均为对应基本类型

floatToIntBits() / doubleToLongBits() : 对符合 IEEE 754 标准的值做对比

JDK 版本 1.8

InterCahce 是对装箱操作的一个优化,缓存部分引用对象,而不是每次都去 new 一个对象(Java 5 引入)。

装箱与拆箱的原理

用一个非常好的例子来说明 int 和 Integer 的区别,以此总结装箱与拆箱的原理。看下面的 demo 你能准确知道输出的值吗?

// Integer 变量实际上是对一个 Integer 对象的引用,所以两个通过 new 生成的 Integer 变量永远是不相等的(因为 new 生成的是两个对象,其内存地址不同)

public static void demo1() {

Integer i = new Integer(100);

Integer j = new Integer(100);

System.out.println(i == j);

System.out.println(i.equals(j));

}

// Integer 变量和 int 变量比较时,只要两个变量的值相等,则结果为 true(因为 Integer 和 int 比较时,会自动拆箱为 int 再比较,实际上是两个 int 变量的比较)

public static void demo2() {

Integer i = new Integer(100);

int j = 100;

System.out.println(i == j);

System.out.println(i.equals(j));

}

// 非 new 生成的 Integer 和 new 生成的变量比较时,结果为 false(因为非 new 生成的变量指向的是 java 常量池中的对象,而 new 生成的变量指向的是 堆 中的对象,两者在内存中的地址不同)

public static void demo3() {

Integer i = new Integer(100);

Integer j = 100;

System.out.println(i == j);

System.out.println(i.equals(j));

}

// 两个非 new 生成的对象,比较时,若值再区间 -128~127 之间,则结果过为 true,否则为 false (因为装箱操作 valueOf() 会缓存这个区间的对象引用,超出重新 new 一个)

public static void demo4() {

Integer i = 100;

Integer j = 100;

System.out.println(i == j);

System.out.println(i.equals(j));

}

public static void demo5() {

Integer i = 128;

Integer j = 128;

System.out.println(i == j);

}

回答:

demo1() 输出 false / true

demo2() 输出 true / true

demo3() 输出 false / true [易错]

demo4() 输出 true / true

demo5() 输出 false / true

重点关注:demo2() - 拆箱;demo3() - 对象存储区;demo4() - 装箱;demo5() - 缓存 。

要知道具体的原因,还得从编译后的 class 文件看起。执行 javac xx.java 编译后得到字节码文件 xx.class,然后对 xx.class 文件执行 javap -v xx 进行反编译,可看到字节码指令,根据指令,可以知道上述 == 语句其实是编译器在编译阶段做了处理,具体整理如下。

demo1() :两个 Integer 引用对象执行 == 操作,比较的是引用地址。因为只要是 new 的对象,都是在堆中申请内存,只要是为不同对象申请内存,肯定不是在地址空间的同一个地方,返回为 false。

demo2():对引用类型 i 比基本类型做 == 操作,底层其实对 i 做了拆箱操作,具体为是拿 i.intValue() 与 j 做对比,也就是 value 值的对比,返回为 true 。

demo3():这里重点不在装箱,在于变量指向的内存地址,new 生成的对象变量指向的内存地址在堆,非 new 生成的对象变量地址指向的常量池中的对象 。

demo4():也是对 i 和 j 在做 == 时做装箱操作,底层其实是 Integer.valueOf(100) == Integer.valueOf(100),而 Integer.valueOf() 的原理是若值在缓存区 -128~127 之间,就直接返回上次缓存的 Integer 值,否则就重新 new 一个。在本例中 100 在区间之类,所以从缓存里获取,返回为 true 。

demo5():返回 false,也是对 i 和 j 在做 == 时做装箱操作,执行 valueOf() 时,因为 128 超出了缓存区间,所以都是重新 new 了一个引用对象,返回 false 。

所以总结以上案例:

执行 == 操作返回值要根据具体场景来判断。如果是两个引用对象做对比,则比较的是引用地址;如果是具体的值,则要根据编译器是做了装箱还是拆箱操作。

装箱操作:valueOf(): Integer,返回引用类型,也要根据值所在区间决定是从缓存取还是重新 new 一个实例;拆箱操作:intValue(): int,返回基本类型。

Java 变量对比,比较的是变量。当 new 一个 Integer ,实际上是生成一个指针指向此对象;而 int 则是直接存储数据值。

备注:

Integer.intValue() 源码实现:

public int intValue() {

return value;

}

Integer.valueOf() 源码实现:

public static Integer valueOf(int i) {

if (i >= IntegerCache.low && i <= IntegerCache.high)

return IntegerCache.cache[i + (-IntegerCache.low)];

return new Integer(i);

}

缓存特性

基于上表可以知道,除了浮点数引用类型,其他的都支持自动装箱时缓存,这种缓存仅在 valueOf() 构建时才有用,使用构建器 new 的 Integer 对象不能被缓存。

IntegerCache 默认支持 -128~127 之间的 int 数字,在 Integer 类第一次被使用时初始化,后续在自动装箱使用 valueOf() 的情况下,就可以直接使用缓存中包含的实例对象,而不是新创建一个。

Byte、Short、Long 固定范围:-128~127。Character 范围为 0~127。仅 Integer 可以支持参数范围改变,可以在 JVM 启动时通过配置参数 -XX:AutoBoxCacheMax=size 读取配置的值。

问题总结

1. Java 中 == 和 equals() 和 hashCode() 的区别。

基本类型,使用 == 比较的是具体的值。

引用类型,使用 == 比较的是内存中存放的地址。new 出来的对象是放在堆中,变量也即是指向对象的指针,存放在栈中(在堆中的地址)。以下两张图看下,会比较好理解。

93bf1b986d80

HotSpot 访问对象

93bf1b986d80

简明内存分配示意图

关于 equals() 是用来判断当前对象和其他对象是否相等,Object 中直接比较对象引用,但是如果子类重写了该方法,就得按照新的规则来判断最后的值。比如上面的 Integer 对象的 equals() 方法,实际上比较的是基本类型的值是否一致;String.equals() 则是先比较长度,再一个个字符比较,如果均相等则相同,也是比较的值。

hashCode() 对象唯一标识。

HashCode的存在主要是为了查找的快捷性,HashCode是用来在散列存储结构中确定对象的存储地址的

如果两个对象equals相等,那么这两个对象的HashCode一定也相同

如果对象的equals方法被重写,那么对象的 HashCode方法也尽量重写

如果两个对象的HashCode相同,不代表两个对象就相同,只能说明这两个对象在散列存储结构中,存放于同一个位置。

2. int、char、long 各占多少字节?

3. int 与 Integer 的区别。

Integer 是 int 的包装类,int 是 java 的一种基本数据类型

Integer 必须实例化后才能使用,int 变量不需要

Integer 变量实际对对象的引用,当 new 一个 Integer 时,实际上是生成一个指向此催下的指针;而 int 则是直接存储数据值

Integer 默认为 null,int 默认为 0 。

其他衍生问题:

自动装箱/拆箱发生在什么阶段?

编译期。

public static void demo6() {

Integer i = 100; // 装箱 - 将基本类型包装成引用类型

int j = i; // 拆箱 - 将引用类型转换为基本类型

}

javap 反编译后的代码:

public static void demo6();

descriptor: ()V

flags: ACC_PUBLIC, ACC_STATIC

Code:

stack=1, locals=2, args_size=0

0: bipush 100

2: invokestatic #8 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;

5: astore_0

6: aload_0

7: invokevirtual #7 // Method java/lang/Integer.intValue:()I

10: istore_1

11: return

LineNumberTable:

line 58: 0

line 59: 6

line 60: 11

可以看出,装箱自动调用的是 Integer.valueOf(int) 方法返回引用类型,拆箱自动调用的是 Integer.intValue() 方法返回的是基本类型。

注:代码中应避免无意义的装箱和拆箱,数量太多会影响性能与内存占用。

使用静态工厂方法 valueOf() 会用到缓存机制,那么自动装箱时,缓存机制起作用吗?

为什么需要原始刷数据类型?

对 Integer 源码的理解。分析下类或某些方法的设计要点。

文章系列:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值