【Java基础】数据类型

基本类型

类型 字节数
boolean ~
byte 1
short 2
int 4
long 8
chart 2
float 4
double 8

boolean有两个值:true和false,但是具体大小没有确定
对于boolean类型,会转换为int;对于boolean数组,会用byte数组来实现

包装类型

基本类型 包装类型
boolean Boolean
byte Byte
short Short
int Integer
long Long
chart Character
float Float
double Double

基本类型与其对应的包装类型之间的赋值使用自动装箱和拆箱完成。

Integer i = 2; // 装箱
int j = i; // 拆箱

等价于

Integer i = Integer.valueOf(2);
int j = i.intValue();
public final class Integer extends Number implements Comparable<Integer> {
    private final int value;

	// 其他属性和方法
}

包装类型包含一个final类型的基本类型,并且提供了一些其他的方法

缓冲池

new Integer(123)Integer.valueOf(123)的区别在于:

  • new Integer(123)每次都会创建一个新对象
  • Integer.valueOf(123)会使用缓冲池中的对象

以前一直以为这些缓冲对象都是由JVM管理的,看了源码才知道是由Java代码管理的。

public final class Boolean implements java.io.Serializable,
                                     Comparable<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);
   }
}
public final class Byte extends Number implements Comparable<Byte> {

   private static class ByteCache {
       private ByteCache(){}

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

       static {
           for(int i = 0; i < cache.length; i++)
               cache[i] = new Byte((byte)(i - 128));
       }
   }
   
   public static Byte valueOf(byte b) {
       final int offset = 128;
       return ByteCache.cache[(int)b + offset];
   }
   
}
public final class Short extends Number implements Comparable<Short> {

   private static class ShortCache {
       private ShortCache(){}

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

       static {
           for(int i = 0; i < cache.length; i++)
               cache[i] = new Short((short)(i - 128));
       }
   }
   
   public static Short valueOf(short s) {
       final int offset = 128;
       int sAsInt = s;
       if (sAsInt >= -128 && sAsInt <= 127) { // must cache
           return ShortCache.cache[sAsInt + offset];
       }
       return new Short(s);
   }
}
public final class Integer extends Number implements Comparable<Integer> {

    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() {}
    }
    
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
}
public final class Long extends Number implements Comparable<Long> {

    private static class LongCache {
        private LongCache(){}

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

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }
    
    public static Long valueOf(long l) {
        final int offset = 128;
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }
}
public final class Character implements java.io.Serializable, Comparable<Character> {

    private static class CharacterCache {
        private CharacterCache(){}

        static final Character cache[] = new Character[127 + 1];

        static {
            for (int i = 0; i < cache.length; i++)
                cache[i] = new Character((char)i);
        }
    }
    
    public static Character valueOf(char c) {
        if (c <= 127) { // must cache
            return CharacterCache.cache[(int)c];
        }
        return new Character(c);
    }
}
public final class Float extends Number implements Comparable<Float> {
    public static Float valueOf(float f) {
        return new Float(f);
    }
}
public final class Double extends Number implements Comparable<Double> {
    public static Double valueOf(double d) {
        return new Double(d);
    }
}

可以看到对应的缓存池如下:

  • Boolean:true 和 false
  • Byte:[-128, 127] 也就是全部
  • Short:[-128, 127]
  • Integer:[-128, 127] (默认)
  • Long:[-128, 127]
  • Character:[0, 127]
  • Float :没有缓存
  • Double :没有缓存

参考

猜你喜欢

转载自blog.csdn.net/qq_21687635/article/details/89499477