包装类基本知识
Java中根本数据类型不是对象,然而咱们在理论利用中常常须要把根本数据转换成对象。所以Java在设计类时为每个根本数据类型设计了一个对应的类进行代表,这样八个和根本数据类型对应的类统称为包装类(Wrapper Class)
包装类均位于java.lang包中
在这八个类中,除了Character和Boolean以外,其余都是“数字型”,“数字型”都是java.lang.Number的子类,Number类是抽象类,因而它的所有子类都须要实现它的形象办法。
Number类提供了形象办法有intValue(),longValue(),floatValue(),doubleValue(),意味着所有数字型包装类都能够相互转型。
包装类用处
1、作为和根本数据类型对应的存在,不便波及到对象的操作,如Object[]、汇合等的操作。
2、蕴含每种根本数据类型的相干属性如最大值、最小值等,以及相干的操作方法(这些操作方法的作用是在根本数据类型、包装类对象、字符串之间提供互相的转化!)
public static void main(String[] args) { //根本类型转换成Integer对象 Integer int1 = new Integer(10); Integer int2 = Integer.valueOf(20);//官网举荐这种写法 //Integer对象转换成int int a = int1.intValue(); //字符串转换成根本数据类型 int int3 = Integer.parseInt("333"); //字符串转换成Integer对象 Integer int4 = new Integer("444"); //Integer对象转换成字符串 String s = int4.toString();}
主动装箱和拆箱
主动装箱和拆箱就是将根本数据类型和包装类进行主动的相互转换。
主动装箱
根本数据类型处于须要对象的环境中时,会主动转为“对象”。
Integer i = 5 这样的语句就能实现根本数据类型转换成包装类,因为JVM为咱们执行了 Integer i = Integer.valueOf(5) 这样的操作,这就是 Java 的主动装箱。
主动拆箱
每当须要一个值时,对象会主动转换成根本数据类型,没必要再去显示调用intValue()、doubleValue()的转型办法。
如 Integer i = 5; int j = i; 这样的过程就是主动拆箱。
总结:
主动装箱过程是通过调用包装类的 valueOf() 办法实现的,而主动拆箱过程是通过调用包装类的 xxxValue() 办法实现的。
主动装箱和拆箱的性能事实上是编译器来帮的忙,编译器在编译时依据所编写的语法,决定是否进行装箱或拆箱动作。
//主动装箱Integer i = 100;//相当于上面的代码Integer i = Integer.valueOf(100);//调用的是valueOf(100),而不是new Integer(100)//主动拆箱int j = i;//相当于上面代码int j = i.intValue();
包装类的缓存问题
整形、char类型所对应的包装类,在主动装箱时,对于-128~127之间的值会进行缓存解决,目标是提高效率。
缓存原理为:如果数据在-128~127这个区间,那么在类加载时就曾经为该区间的每个数值创立了对象,并将这256个对象寄存到一个名为cache的数组中。每当主动装箱产生时(或者调用valueOf办法时),就会先判断数据是否在该区间,如果在间接获取数组中对应的包装类对象的援用,如果不在该区间,则会通过new调用包装类的构造方法来创建对象。
Integer类相干源码如下
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);}
1、IntegerCache类为Integer类的一个动态外部类,仅供Integer类应用
2、个别状况下 IntegerCache.low 为-128, IntegerCache.high 为127, IntegerCache.cache 为外部类的一个动态属性,代码如下
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() {}}
由下面的源码能够看到,动态代码块的目标就是初始化cache数组的,这个过程会在类加载时实现。
测试代码:
public class Test { public static void main(String[] args) { Integer int1 = 123; Integer int2 = 123; System.out.println(int1 == int2);//true,因为123在缓存范畴内 System.out.println(int1.equals(int2));//true Integer int3 = 1234; Integer int4 = 1234; System.out.println(int3 == int4);//false,因为1234不在缓存范畴内 System.out.println(int3.equals(int4));//true }}