概述
Java的数据类型分为基本数据类型
和引用数据类型
。
使用关键字new
来创建对象,创建的对象都存放在内存的堆
中。而对于使用new
来创建占用空间小的或者简单的变量来说,会显得比较啰嗦,所以就出现了Primitive type
。他们的值存放在内存的堆栈
中。Java为每种基本数据类型定义了存储空间大小,这种大小是固定的,不会随机器架构的不同而改变。
Primitive type | Size | Scope | Wrapper type |
---|---|---|---|
boolean | - | - | Boolean |
byte | 8 bits | -128 ~ 127 | Byte |
short | 16 bits | -215 ~ 215 -1 | Short |
char | 16 bits | -215 ~ 215 -1 | Character |
int | 32 bits | -231 ~ 231 -1 | Integer |
long | 64 bits | -263 ~ 263 -1 | Long |
float | 32 bits | -231 ~ 231 -1 | Float |
double | 64 bits | -263 ~ 263 -1 | Double |
拆箱与装箱
由上可知:
- Primitive types --> stack
- Reference types(new places objects) --> heap
装箱
基本数据类型 —> 对象类型
自动装箱的实现步骤为:
Integer num = 128;
==> Integer num = Integer.valueOf(128);
==> Integer num = new Integer(128);
JDK中Integer.valueOf()源码如下:
public static Integer valueOf(int i) {
if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
return new Integer(i);
}
拆箱
对象 —> 基本数据类型
自动拆箱的实现步骤为:
源码:
Integer num = 10;
int a = num;
编译后的代码:
Integer num = Integer.valueOf(10);
int a = num.intValue();
在拆箱的过程中,其内部调用的是 Integer中的intValue() 方法。
int与Integer比较
public static void test2() {
boolean result;
//
int num1 = 128;
int num2 = 128;
result = num1 == num2;
// 输出结果:true
System.out.println("int == int: " + result);
//
//
Integer num3 = 127;
Integer num4 = 127;
result = num3 == num4;
// 输出结果:true
System.out.println("Integer == Integer: " + result);
//
//
Integer num5 = 128;
Integer num6 = 128;
result = num5 == num6;
// 输出结果:false
System.out.println("Integer == Integer: " + result);
//
}
- 基本数据类型在stack中,如果存在了值,则直接获取该值;如果不存在,则在stack中添加该值,所以num1 == num2。
- 要理解num3 == num4结果为true,而num5 == num6结果为false,则要看Integer.valueOf()源码。
public static Integer valueOf(int i) {
if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[]; // 创建一个Integer对象数组
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]; // 设置数组大小为256
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 Integer.IntegerCache.high >= 127;
}
private IntegerCache() {}
}
由上代码可知,在自动装箱的时候,如果int值在[-128, 127]之间,那么装箱的时候,直接赋值一个Integer对象,所以num3 == num4结果为true,而num5 == num6结果为false。再来看以下例子:
public static void test1() {
boolean result; // 判断结果
//
Integer num1 = new Integer(128);
int num2 = 128;
result = num1 == num2;
// 输出结果:true
System.out.println("new Integer == int: " + result);
//
//
int num3 = 150;
Integer num4 = new Integer(150);
result = num3 == num4;
// 输出结果:true
System.out.println("int == new Integer: " + result);
//
//
int num = 127;
Integer num5 = new Integer(num);
Integer num6 = new Integer(num);
result = num5 == num6;
// 输出结果:false
System.out.println("new Integer == new Integer: " + result);
//
}
int与Integer在使用==
比较时,会先把Integer自动拆箱,然后比较两个值是否相等。
Integer num1 = new Integer(128);
int num2 = 128;
result = (num1.intValue() == num2);
范围越界
是否存在 x>x+1?为什么?
public static void main(String[] args) {
byte a = 127;
a += 5;
System.out.println(a); // -124
}
当超过最大临界值后,会变为最小值。