Java 包装类型及其特性

自动装箱和自动拆箱

  • 装箱:将基础类型转化为包装类型。
  • 拆箱:将包装类型转化为基础类型。

举例

Integer i = 10;  //装箱
int n = i;   //拆箱

从字节码文件中我们发现,装箱其实就是调用了包装类的valueOf()方法拆箱其实就是调用了xxxValue()方法
即:
Integer i = 10 等价于 Integer i = Integer.valueOf(10)
int n = i 等价于 int n = i.intValue()

注意:如果频繁拆装箱的话,也会严重影响系统的性能。我们应该尽量避免不必要的拆装箱操作。

包装类型缓存机制

整数包装类型源码:

//Integer
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 {
        // high value may be configured by property
        int h = 127;
    }
}
  • Byte、Short、Integer、Long 这 4 种包装类默认创建了数值 [-128, 127] 闭区间的相应类型的缓存数据。

  • Character 包装类型创建了数值在 [0, 127] 闭区间范围的缓存数据。

  • Boolean 包装类型直接返回 True 或者 False。

  • 两种浮点数类型的包装类 Float,Double 并没有实现缓存机制。

在创建包装类型对象时,如果数值在缓存区间内,则直接返回cache[]数组中的对象引用;如果超出缓存区间,则创建一个新的对象。

举例

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

Float i11 = 333f;
Float i22 = 333f;
System.out.println(i11 == i22);// false

Double i3 = 1.2;
Double i4 = 1.2;
System.out.println(i3 == i4);// false

综合案例

Integer a1 = 1;
Integer b1 = new Integer(1);
Integer a2 = 1;
Integer b2 = new Integer (1);
int c2 = 1;
int d2 = new Integer (1);
Integer e2 = null;

System.out.println(a1 == a2);// true
System.out.println(a1 == b2);// false
System.out.println(a1 == c2);// true
System.out.println(a1 == d2);// true
System.out.println(b1 == a2);// false
System.out.println(b1 == b2);// false
System.out.println(b1 == c2);// true
System.out.println(b1 == d2);// true
System.out.println(e2 == 1);// java.lang.NullPointerException
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值