基本類型所占用的字節(jié)
- 1byte = 8bit
- byte 1個字節(jié)
- short、char 2個字節(jié)
- int、float 4個字節(jié)
- long、double 8個字節(jié)
- boolean 1個字節(jié)

java的基本類型
Integer、Short、Byte、Character、Long這幾個類的valueOf方法的實現(xiàn)是類似的。
以Integer為例,其valueOf(int i)的源代碼為:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
在Integer里面IntegerCache.low是固定的-128,IntegerCache.high默認是127,但是也可以通過-XX:AutoBoxCacheMax=<size>來配置,而Short、Byte、Character、Long的緩存大小都是固定在-128~127的。
Double、Float的valueOf方法也是類似的。
以Double 為例,其valueOf(double d)的源代碼為:
public static Double valueOf(double d) {
return new Double(d);
}
Double的valueOf方法每次返回的都是新的對象。
Boolean的valueOf方法比較特殊。
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
Boolean因為只有true和false兩種情況,所以在加載的時候就會創(chuàng)建TRUE和FALSE兩個對象。自動裝箱的時候則返回這兩個對象的其中一個。