一、Java数据类型

1、在说装箱与拆箱之前,先说一下Java的基本数据类型,Java从数据类型上可以划分为值类型与引用类型,值类型是四类八种,分别是:
| 数据类型 | 内存 | 默认值 | 包装类 | 
|---|---|---|---|
| byte | 8位 | 0 | Byte | 
| short | 16位 | 0 | short | 
| int | 32位 | 0 | Integer | 
| long | 64位 | 0L或0l | Long | 
| float | 32位 | 0.0F或0.0f | Float | 
| double | 64位 | 0.0D或0.0d | Double | 
| char | 16位 | \u0000 | Character | 
| boolean | 8位 | flase | Boolean | 
2、引用类型:
3、值类型与引用类型的区别
1. 从概念方面上来说:
2. 从内存构建方面上来说:
3. 从使用方面上来说:
二、Java数据类型转换
1、自动转换
运算中,不同类型的数据先转化为同一类型,然后进行运算
| 操作数1类型 | 操作数2类型 | 转换后的类型 | 
|---|---|---|
| byte、short、char | int | int | 
| byte、short、char、int | long | long | 
| byte、short、char、int、long | float | float | 
| byte、short、char、int、long、float | double | double | 
2、强制转换
- int x;
 - double y;
 - x = (int)3.14 + (int)5.20 //精度丢失
 - y = (double)x + (double)8 //精度提升
 - 输出:x = 8;y = 16.0
 
三、Java之装箱与拆箱
1、包装类
2、什么是装箱与拆箱
- int x = 3;
 - Integer y = x; //int --> Integer,Integer y = x <==> Integer y = Integer.valueOf(x)
 
- Integer x = new Integer(5);
 - int y = x; //Integer --> int,int y = x <==> int y = x.intValue()
 
3、装箱和拆箱是如何实现的
4、注意点:
- List
 list = new ArrayList<>(); - Integer x,y,z;
 - x = 1;y = 2;z = 4;
 - list.add(x);list.add(y);list.add(z);
 - list.remove(2);
 
在上面这段代码中ArrayList.remove方法有两个重载方法,那么list.remove(2)是调用了哪个方法,remove掉的是值为2的对象,还是remove了index为2,值为4的那个对象呢?
在这种情况下,编译器不会进行自动拆装箱,所以调用的是remove(int index),index为2值为4的这个Integer对象会被remove.
如果要调用 remove(Object o)的方法,应该这么写 list.remove(y)
3. 缓存值问题
- Integer i1 = 100;
 - Integer i2 = 100;
 - Integer i3 = 200;
 - Integer i4 = 200;
 - System.out.println(i1==i2);
 - System.out.println(i3==i4);
 - Output: true false
 
Intteger.valueOf方法
- public static Integer valueOf(int i) {
 - if (i >= IntegerCache.low && i <= IntegerCache.high)
 - return IntegerCache.cache[i + (-IntegerCache.low)];
 - return new Integer(i);
 - }
 
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.
 - }
 - }
 - hhigh = 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方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象
| 包装类 | 常量池 | 常量池范围 | 
|---|---|---|
| Byte | 存在 | [-128,127] | 
| Short | 存在 | [-128,127] | 
| Integer | 存在 | [-128,127] | 
| Long | 存在 | [-128,127] | 
| Character | 存在 | [0,127] | 
| Float | 不存在 | 无 | 
| Double | 不存在 | 无 |