java 包装类详解

包装类

包装类结构图

一、装箱

将基本数据类型转换为包装类类型

通过构造方法( JDK1.5之前)

	Integer integer = new Integer(12);      // 12
       
    Integer integer1 = new Integer("12");       // 12
        
    Integer integer2 = new Integer('a');    // 97
       
    //Integer integer3 = new Integer("a");      // NumberFormatException
    
    System.out.println(integer == integer1);        // false
    System.out.println(integer.equals(integer1));       // true

通过valueOf()方法

	Integer integer4 = Integer.valueOf(12);     // 12
        
    Integer integer5 = Integer.valueOf("12");       // 12

    Integer integer6 = Integer.valueOf('a');        // 97
		
	//Integer integer7 = Integer.valueOf("a");     // NumberFormatException

	System.out.println(integer4 == integer5);        // true
    System.out.println(integer4.equals(integer5));       // true

超范围演示

	Integer integer4 = Integer.valueOf(300);
    Integer integer5 = Integer.valueOf(300);
    System.out.println(integer4 == integer5);        // false
    System.out.println(integer4.equals(integer5));       // true

二、拆箱

将包装类类型转换为基本数据类型

通过xxxValue()方法

	Character ch = 'f';
    char value = ch.charValue();        // f

三、自动装箱(JDK 1.5+)

将基本数据类型自动转换为包装类类型

	Integer num1 = 100;		// 自动装箱

底层调用Integer num1 = Integer.valueOf(100);

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

判断值在[-128,127]之间,则执行IntegerCache.cache[i + (-IntegerCache.low)];,否则直接创建一个新的Integer对象。

Integer的构造方法

private final int value;

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

public Integer(String string) throws NumberFormatException {
    this(parseInt(string));
}

IntegerCache.cache[i + (-IntegerCache.low)];的底层实现

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

源码解析:

  • 声明三个静态常量
  • 静态代码块
    • ① 定义变量h并赋值127
    • ② 获取一个String类型的integerCacheHighPropValue值
    • ③ 判断integerCacheHighPropValue值是否为null
      • ④ 不为null时解析String值为int值赋给变量i,为null时直接跳转到 ⑦ high = 127
      • ⑤ i值与127取最大值重新赋给变量i
      • ⑥ i值与Integer.MAX_VALUE - (-low) -1取最小值重新赋给变量h
    • ⑦ 变量h值赋给high
    • ⑧ new一个Integer类型的cache数组,长度为(high - low) + 1
    • ⑨ low赋给j变量
    • ⑩ for循环实例化数组
  • 私有构造方法

总结:通过valueOf()创建Integer对象时,数值在[ -128,127 ]之间,则直接返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

装箱的过程会创建对应的对象,会消耗内存,装箱的过程会增加内存的消耗,影响性能。

@ example:

public class Main {
    public static void main(String[] args) {
        Integer i1 = 100;
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;

        System.out.println(i1==i2);	 // true(引用到了同一个Integer对象)
        System.out.println(i3==i4);	 // false(执行new Integer(200),会分别创建两个不同的对象)
    }
}
public class Main {
    public static void main(String[] args) {
        Double i1 = 100.0;
        Double i2 = 100.0;
        Double i3 = 200.0;
        Double i4 = 200.0;

        System.out.println(i1==i2);  //false
        System.out.println(i3==i4);  //false
    }
}
public class Main {
    public static void main(String[] args) {

        Boolean i1 = false;
        Boolean i2 = false;
        Boolean i3 = true;
        Boolean i4 = true;

        System.out.println(i1==i2);  //true
        System.out.println(i3==i4);  //true
    }
}

对于Integer类型来说,在[ -128,127 ]之间只有固定的256个值,已经事先创建好大小为256的cache数组,如果值在范围内,就直接返回已创建好的对象引用即可。若不在范围内,则创建一个新的Integer对象。

对于Double类型来说,范围内个数是无限的,因此每次直接创建一个对象。

	public static Double valueOf(double d) {
    	return new Double(d);
	}

对于Boolean类型,因为只有两种情况,故内部已经提前创建好两个对象

	public static final Boolean TRUE = new Boolean(true);
	
	public static final Boolean FALSE = new Boolean(false);

	public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

总结:

  • Integer、Short、Byte、Character、Long类型的valueOf方法的实现是类似的。
  • Double、Float的valueOf方法的实现是类似的,每次都返回不同的对象。

在这里插入图片描述

四、自动拆箱(JDK 1.5+)

将包装类类型自动转换为基本数据类型

	Integer num1 = 100;
    int num2 = num1;		// 自动拆箱

执行int num2 = num1; 时底层调用int num2 = num1.intValue();

	public int intValue() {
        return value;
    }

五、自动拆箱与装箱具体细节

(1)表达式参数为基础数据类型与包装类类型
	Integer num1 = 400;
    int num2 = 400;
    System.out.println(num1 == num2);     //true     num1 == num2进行了拆箱操作
    System.out.println(num1 + num2);        // 800   num1拆箱之后和num2做加法

总结:当一个基础数据类型与包装类类型进行 ==+-*/ 运算时,会将包装类进行拆箱,然后对基础数据类型进行运算。

	Integer num3 = 100;
    int num4 = 100;
    Long num5 = 200L;
    System.out.println(num3.equals(num4));      //true    参数为int类型,首先会进行装箱Integer,然后比较value
    
    System.out.println(num5.equals(num3 + num4));        //false  类型不同 Long != Integer   
    
    System.out.println(num3.equals(num5));          //false   ((Integer)obj).intValue();

    System.out.println(num5 == (num3 + num4));      //true   num5拆箱,然后基本类型比较value

总结:

  • ① 若传入的是基本数据类型值,则先进行装箱,然后用instanceof判断,为true则强转后调用inValue(),最后比较是否相等;为false则返回false
  • ② 若传入的是表达式,则先计算表达式,产生的结果使用规则 ①
  • ③ 若传入的是包装类类型,则直接用instanceof判断,为true则强转后调用inValue(),最后比较是否相等;为false则返回false

★★★ 对于equals()比较必须满足两个条件才为true

  • 类型要相同
  • 内容要相同

equals( ) 源码

	public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
	public boolean equals(Object obj) {
        if (obj instanceof Long) {
            return value == ((Long)obj).longValue();
        }
        return false;
    }
(二)表达式参数均为包装类型
	Integer num6 = 200;
    Integer num7 = 200;
    Long num8 = 400L;
    Integer num9 = 400;
    System.out.println(num6 == num7);       // false   超范围,故new Integer()
    
    System.out.println(num6.equals(num7));     // true     Integer == Integer && 400 == 400
    
    System.out.println(num8.equals(num9));      // false    Long != Integer
    
    System.out.println(num8 == (num6 + num7));     // true    400 == 400 【特别注意】

总结:当 == 表达式两边的操作数均为包装类型时才比较的是引用(如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)),equals()比较规则依然不变(满足两个条件)。

(三)特例
	Integer num10 =null;
    //int num10 = num10;       //NullPointerException

编译可以通过,但运行时会抛出空指针异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值