Java包装类

1.每个基本类型都有一个对应的包装类。

2.Java中基本数据类型不是面向对象的,使用对应的包装类方便对象的操作。

3.每个基本数据类型对应的包装类,可以使用对应的基本数据类型的构造函数

public Integer(int value) {
  this.value = value;
}

除Character外,其他的包装类可以使用String作为参数的构造函数

4.JDK1.5新增自动装箱和自动拆箱的功能

Integer i = 100; 将源码反编译,可以看到编译器已经优化了 -->  Integer i = Integer.valueOf(100);即自动装箱

int i = new Integer("123");将源码反编译 -->  int i = new Integer("123").intValue();即自动拆箱

5.对于Integer类,

该类有一个静态内部类 IntegerCache  会缓存[-128,127]的Integer对象,

@Test
    public void testApp(){

        Integer i = 100;  // 调用valueOf
        Integer j = 100;
        System.out.println(i == j);    //true
        System.out.println(i.equals(j));  //true
        Integer i1 = 128;
        Integer j1 = 128;
        System.out.println(i1 == j1);  //false
        System.out.println(i1.equals(j1)); //true

    }

 

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)]; // [-128,127] 返回缓存对象
        return new Integer(i);  // 其他直接返回new 对象
    }

 

posted on 2017-02-21 17:13 Gavin_Yi 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/gavin2016/p/6425097.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值