原始/基本数据类型
| byte | 1 字节 | -128 ~ 127 |
|---|---|---|
| boolean | 1 字节 | true false |
| short | 2 字节 | -2^15 ~ 2^15-1 |
| char | 2 字节 | ‘\u0000’ ~ ‘\uffff’ |
| int | 4 字节 | -2^31 ~ 2^31-1 |
| float | 4 字节 | -3.4E38~3.4E38 |
| long | 8 字节 | -2^63 ~ 2^63-1 |
| double | 8 字节 | -1.7E308~1.7E308 |
引用数据类型
- Boolean
- Byte
- Short
- Character
- Integer
- Float
- Double
- Long
自动拆箱装箱
Int/Integer 自动装箱/拆箱
Java 替我们自动把装箱转换为 Integer.valueOf(),把拆箱替换为 Integer.intValue()

其他类型类似 :
装箱转换为 Boolean/Character/Double/Long/Float/Short/Byte.valueOf() 方法
拆箱转换为
- Boolean.booleanValue();
- Character.charValue();
- Double.doubleValue();
- Long.longValue();
- Float.floatValue();
- Short.shortValue();
- Byte.byteValue();
Integer 的缓存
关于 Integer 的值缓存,这涉及 Java5 中另一个改进。构建 Integer 对象的传统方式是直
接调用构造器,直接 new 一个对象。但是根据实践,我们发现大部分数据操作都是集中在
有限的、较小的数值范围。因此在 Java5 中新增了静态工厂方法 valueof(),在调用它的
时候会利用一个缓存机制,带来了明显的性能改进。按照 Javadoc,这个值默认缓存是
-128 到 127 之间。
public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}
private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h = 127;String integerCacheHighPropValue =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_VALUEh = 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() {}}

这种缓存机制并不是只有 Integer 才有,同样存在于其他的一些包装类。比如:
- Boolean,缓存了
true / false对应实例,确切说,只会返回两个常量实例 Boolean TRUE / FALSE

- Short,同样是缓存了 -128 到 127 之间的数值

- Byte,数值有限,所以全部都被缓存


- Character,缓存范围 ‘\u0000’ 到 ‘\u007F’

