Java基础知识

java基础2. 基本数据类型的封装类

2019-03-15  本文已影响0人  ygxing
1.基本类型和封装类型的对应表格
数据类型 关键字 封装类
布尔类型 boolean Boolean
字节类型 byte Byte
字符型 char Character
短整型 short Short
整形 int Integer
单精度浮点数 float Float
长整形 long Long
双精度浮点数 double Double
2.基本类型和封装类型的 == 判断
-封装类型和基本类型比较
封装类型会进行自动拆箱,然后变成基本类型判断
public static void main(String[] args) {
    Integer num1 = 1000;
    int num2 = 1000;
    System.out.println("Integer == int : " 
        + (num1 == num2));
}
运行结果:
    Integer == int : true
-封装类型和封装类型比较
两个封装类型进行 "=="比较的时候, 判断的是两个指针引用是否指向同一个地址
  public static void main(String[] args) {
    Integer num1 = 127;
    Integer num2 = 127;
    System.out.println("Integer == Integer : " 
        + (num1 == num2));
  }
运行结果:
    Integer == Integer : true
  public static void main(String[] args) {
    Integer num1 = 1000;
    Integer num2 = 1000;
    System.out.println("Integer == Integer : " 
        + (num1 == num2));
  }
运行结果:
    Integer == Integer : false
3.基本类型和封装类型的 equals 判断
封装类型和基本类型进行equals比较的时候,
基本类型装箱会先进行装箱操作,
转换为封装类型,
然后进行equals判断
 public static void main(String[] args) {
    int num1 = 1000;
    Integer num2 = 1000;
    System.out.println("Integer.equals(int) : " 
        + num2.equals(num1));
  }
运行结果:
    Integer.equals(int) : true
4.整型常亮缓存池
Byte,Short,Integer,Long这四种类型
分别维护了一个私有的静态内部缓存类,
在缓存类中,缓存了一个范围是[-128,127]的数组;
当我们使用valueOf()方法或者用整型直接赋值的时候,
会判断数据范围,如果是在缓存范围内,
那么优先从缓存中取出来
Byte缓存源码
Byte,Short,Long这三个封装类的思想基本上是一样的
/**
 * 私有静态内部缓存类 
 */
private static class ByteCache {
    private ByteCache(){}

    static final Byte cache[] = new Byte[-(-128) + 127 + 1];

    //静态代码块,将-128到127范围内的数据缓存的cache数组中
    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Byte((byte)(i - 128));
    }
}

/**
 *调用valueOf方法的时候,直接从缓存数组中取出来 
 */
public static Byte valueOf(byte b) {
    final int offset = 128;
    return ByteCache.cache[(int)b + offset];
}

Integer缓存源码
Integer的缓存实现和Byte有所不同,
缓存的最大值需要从JVM中取出

/**
 * 私有静态内部缓存类 
 */
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];

    static {
        //从jvm中取出关于缓存最大值的配置
        //java规范中规定,[-128, 127]必须缓存
        //所以此处缓存最大值是Math.max(i, 127)
        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) {
               
            }
        }
        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() {}
}

/**
 * 调用valueOf方法的时候先判断数值的大小,
 * 如果是在缓存的范围中,那么直接从缓存数组中取出来 
 */
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
上一篇 下一篇

猜你喜欢

热点阅读