字符串常量池和包装类缓冲池中数据的存储方式
准备知识
常量池和缓冲池本质都是一个存放特定数据的内存空间, 所以先来分析内存空间
内存空间分类:
栈, 堆, 方法区
存放的数据:
1. 栈: 基本数据类型的数据, 引用数据类型的引用(地址)
2. 堆: 类的对象
3. 方法区: 类的信息 (静态的变量, 常量, 成员方法)
System.out.println(a):
1. 若a为基本数据类型, 打印的是变量a所指向的对象内的值
2. 若a为引用数据类型(即类对象), 打印的是在变量a存放的地址值, 需要对类重写toString方法.
事实上: 看过源码就知道,如果a是基本数据类型, println()方法将a装箱成了相应类型的包装类,
然后调用包装类中已重写好的toString方法, 所以打印的是对象内的值, 而不是地址.
**注释:
指向: 将变量赋于对象的内存地址
**对象: 存放有**数据的对象
== : 比较的是左右两个变量存放的地址
基础数据类型存储方式(以int为例):
1. int a = 1;
先在栈内存中查找是否有1这个对象, 如果有则将a指向1;
如果没有就创建一个1对象, 将a指向这个对象
例子:
int a = 1;
int b = 1;
a = 2;
先在栈内存中查找是否有2这个对象, 如果有则将a指向2;
如果没有就创建一个2对象, 将a指向这个对象
int c = 2;
System.out.println(a);//a变为2
System.out.println(b);//b还是1
System.out.println(a == c);//true
上图:
字符串的存储方式(字符串常量池)
2. String s1 = "abc";
在字符串常量池中查找是否有"abc"对象, 如果没有则创建"abc"再将s1指向"abc";
如果有就直接将s1指向"abc";
String s2 = new String("efg");
直接在堆中创建"efg"对象, 然后将栈中的s2指向"efg"
例子1:
String s1 = "abc";
String s2 = "abc";//将s1存放的地址赋值给s2, 则s2也指向了"abc"对象
System.out.println(s1 == s2);//true
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s3 == s4);//false
原理:类似于基本数据类型, 只是数据存放的位置不一样, 基本数据类型存放在栈;
字符串变量存放在栈, 对象存放在堆.
包装类的存储方式(包装类缓冲池)(以Integer为例)
3. Integer i = 1;
等号右边的数若在[-128,127], 则先将其装箱成包装类,然后存放在Integer缓冲池中
(看源码可以知道, 其实就是存放在Integer类型的数组中).
先查看缓冲池中是否有该Integer对象, 若有则将变量i指向它;
若没有则在缓冲池中创建Integer对象,在将变量i指向它.
Integer j = 200;
因为右边的数不在[-128,127], 直接在堆内存中创建Integer对象, 将变量j指向它.
例子:
Integer a = 1;
Integer b = 1;
System.out.println(a == b);//true
Integer c = 200;
Integer d = 200;
System.out.println(c == d);//false
上图:
源码参考
//println打印基础数据类型, 先装箱成包装类, 在调用包装类toString方法
public void println(int x) {
synchronized (this) {
print(x);
newLine();
}
}
public void print(int i) {
write(String.valueOf(i));
}
public static String valueOf(int i) {
return Integer.toString(i);
}
//Integer i = 1; 就是Integer i = Integer.valueOf(1)
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[];//缓冲池, Integer数组
static {
// high value may be configured by property
int 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_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() {}
}