java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java封装类和装箱拆箱

Java的封装类和装箱拆箱详解

作者:才尽散人

Java中存在基础数据类型,但是在某些情况下,我们要对基础数据类型进行对象的操作,例如,集合中只能存对象,而不能存在基础数据类型,于是便出现了封装类,本文将详细给大家介绍Java封装类和装箱拆箱,需要的朋友可以参考下

一、封装类

1.封装类概念

Java中存在基础数据类型,但是在某些情况下,我们要对基础数据类型进行对象的操作,例如,集合中只能存对象,而不能存在基础数据类型,于是便出现了封装类。封装类就是对基本数据类型进行封装,并用它生成对象,以便以对象方式操作基本数据类型。每一个基本数据类型都对应一种封装类。

2. 各个基础类型对应的封装类

基础类型封装类型
intInteger
byteByte
shortShort
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter

二、装箱与拆箱

1.装箱与拆箱概念

2.基础数据类型封装

public class Test {
    public static void main(String[] args) {
        int num = 1;
        Object obj = new Num(num);//父类型引用指向子类型对象
        System.out.println(obj);
    }
}
public class Num {
    int num;
 
    public Num(int num) {
        this.num = num;
    }
 
    public String toString() {
        return "" + num;
    }
}
//实现封装

3.自动装箱拆箱演示

public class Test {
    public static void main(String[] args) {
        int num = 10;
 
        Integer num1 = num; // 自动装箱
        int num2 = num1; // 自动拆箱
    }
}

上面代码中,首先,num自动装箱为Integer类对象赋值给num1;然后,num1又自动拆箱为基本数据类型。

4.Integer中valueOf方法和 intValue方法源码

Integer在装箱过程中调用了Integer中的valueOf方法,拆箱时调用了Integer中的intValue方法。

valueOf方法:

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

intValue方法:

    public int intValue() {
        return value;
    }

三、自动装箱和拆箱中的一些问题

1.相同数值比较返回值为false

public class Main {
    public static void main(String[] args) {
        Integer num1 = 100;
        Integer num2 = 100;
        Integer num3 = 200;
        Integer num4 = 200;
        System.out.println(num1 == num2);
        System.out.println(num3 == num4);
    }
}
/*
 * 运行结果
 * true
 * false
 */

源码:

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

从源码中可以看出,在IntegerCache类中初始化了一个Integer数组,它的范围为-128到127。num1==num2在-128到127之间,因此给num1和num2赋值时,直接返回cache[ ]数组中的对象,属于同一个对象,返回值为true;而200超过了这个范围,给num3和num4赋值时,直接返回new Integer(),因此属于两个不同的对象,返回值false。

2.浮点型数值比较返回值为false7

public class Main {
    public static void main(String[] args) {
        Double num1 = 100.0;
        Double num2 = 100.0;
        Double num3 = 200.0;
        Double num4 = 200.0;
        System.out.println(num1 == num2);
        System.out.println(num3 == num4);
    }
}
/*
 * 运行结果
 * false
 * false
 */

源码:

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

从源码中可以看出,Double中的valueOf()返回了一个新的封装类对象,因此都返回false。

以上就是Java的封装类和装箱拆箱详解的详细内容,更多关于Java封装类和装箱拆箱的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文