编程语言
首页 > 编程语言> > JAVA常用类--包装类

JAVA常用类--包装类

作者:互联网

Java常用类整理

包装类

Integer_Number

public class Test1 {
    public static void main(String[] args) {
        //基本数据类型转换成包装类对象
        Integer a = new Integer(3);
        Integer b = Integer.valueOf(30);

        //把包装类对象转成基本数据类型
        int c = b.intValue();
        double d = b.doubleValue();

        //把字符串转成包装类对象
        Integer e = new Integer("9999");
        Integer f = Integer.parseInt("999888");

        //把包装类对象转成字符串
        String str =f.toString();   //“ ”+f

        //常见的常量
        System.out.println("int类型最大的常量:"+Integer.MAX_VALUE);
    }
}

自动装箱和拆箱

自动装箱和拆箱的功能实际上是编译器来帮忙,编译器在编译时依据你所写的语法,决定是否进行装箱或者拆箱动作

public class Test2 {
    public static void main(String[] args) {
        Integer a = 234;  //自动装箱。Integer a = Integer.valueOf(234);
        int b = a;        //自动拆箱。编译器会自动改成int b = a.intValue();

        Integer c = null;
        if(c!=null){
            int d = c;
        }
       // int d = c;        //自动拆箱:调用了c.intValue(),解决办法:改成if
    }
}

包装类的缓存问题

public class Test3 {
    public static void main(String[] args) {
        //缓存[-128,127]之间的数字,实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组
        //当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经创建好的对象。
        //如果不在这个范围,则创建新的Integer对象。
        Integer in1 = -128;//Integer in1 = Integer.valueOf(-128)
        Integer in2 = -128;
        System.out.println(in1 == in2);//true 因为123在缓存范围内
        System.out.println(in1.equals(in2));//true
        System.out.println("#################");
        Integer in3 = 1234;
        Integer in4 = 1234;
        System.out.println(in3 == in4);//false 因为1234不在缓存范围内
        System.out.println(in3.equals(in4));//true
    }
}

输出结果:
在这里插入图片描述
System.out.println(in1 == in2);为true的原因:

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为内部类的一个静态属性。

IntegerCache类相关源码如下:

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的,这个过程会在类加载时完成。以下为Test3内存分析图。
在这里插入图片描述
注意:

  1. JDK1.5以后,增加了自动装箱与拆箱功能,如:
Integer i = 100;  int j = new Integer(100);
  1. 自动装箱调用的是valueOf()方法,而不是new Integer()方法。
  2. 自动拆箱调用的xxxValue()方法。
  3. 包装类在自动装箱时为了提高效率,对于-128~127之间的值会进行缓存处理。超过范围后,对象之间不能再使用==进行数值的比较,而是使用equals方法。

标签:static,JAVA,--,包装,int,IntegerCache,127,128,Integer
来源: https://blog.csdn.net/qq_45707027/article/details/111242905