Class常量池
Class常量池可以理解为是Class文件中的资源仓库。 Class文件中除了包含类的版本、字段、方法、接口等描述信息外,还有一项信息就是常量池(constant pool table),用于存放编译期生成的各种字面量(Literal)和符号引用(Symbolic References)。
字面量
字面量就是指由字母、数字等构成的字符串或者数值常量
字面量只可以右值出现,所谓右值是指等号右边的值,如:int a=1 这里的a为左值,1为右值。在这个例子中1就是字面量。
符号引用
相对于直接引用来说,包含以下三类常量:
- 类和接口的全限定名
- 字段的名称和描述符
- 方法的名称和描述符
运行时常量池
这些Class文件中的常量池现在是静态信息,当程序运行被加载到内存后,这些符号引用才有对应的内存地址信息,并且常量池会变成运行时常量池,对应的符号引用会被转变为加载到内存区域的代码的直接引用,也就是动态链接。
比如test()这个符号引用在程序运行时会被转变为test()方法具体代码在内存中的地址,主要通过对象头里的类型指针去转换直接引用。
字符串常量池
设计思想
字符串的分配,和其它对象分配一样,耗费空间和时间,作为基础数据类型,大量频繁的创建字符串会影响程序性能,所以JVM为了提升性能减少内存开销,在实例化字符串常量时做了一些优化:
- 为字符串开辟一个字符串常量池
- 创建字符串常量时,首先查询字符串常量池是否存在该字符串
- 存在该字符串,返回常量池里的引用,不存在则实例化字符串放入池中
字符串操作
- 直接赋值字符串
String s = "test"; // s指向常量池中的引用
创建的字符串只会在字符串常量池中,创建之前会用equals(key)判断是否有这个字符串
有则直接返回池中的引用,没有则创建在返回引用
- new String();
String s = new String("test"); // s指向内存中的对象引用
创建对象之前会判断池中是否有这个字面量的字符串,没有则创建,最后在堆里创建对象返回引用
- intern方法
String s1 = new String("test");
String s2 = s1.intern();
System.out.println(s1 == s2); //false
判断池中是否有这个字符串,有则返回池中的引用,没有则将返回的引用指向s1这个堆里的对象
字符串常量池位置
JDK1.7及以后字符串常量池在堆里,运行时常量池在元空间或永久代里
例子
String s0="hello";
String s1="hello";
String s2="he" + "llo";
System.out.println( s0==s1 ); //true
System.out.println( s0==s2 ); //true
s0和s1的字面量都是字符串常量,在编译器已经确定,s2是两个字符串常量相加,编译器也会优化为字符串常量,都指向常量池里的同一个字符串,所以都相等。
String s0="hello";
String s1=new String("hello");
String s2="he" + new String("llo");
System.out.println( s0==s1 ); // false
System.out.println( s0==s2 ); // false
System.out.println( s1==s2 ); // false
s0指向常量池里的,s1指向堆里新创建的对象,new String()创建的不是常量 无法在编译期确定,s2是字符串常量加新创建的对象,编译期无法优化,会新创建一个对象,所以都不相等。
String a = "a1";
String b = "a" + 1;
System.out.println(a == b); // true
String a = "a1.0";
String b = "a" + 1.0;
System.out.println(a == b); // true
字符串常量的加号在编译期就被优化为相加后的值,所以都相等
String a = "ab";
String b = "b";
String b = "a" + b;
System.out.println(a == b); // false
字符串常量加引用,而引用的值在程序编译期是无法确定的,即"a" + b无法被编译器优化,只有在程序运行期来动态分配并将连接后的新地址赋给b。所以上面程序的结果也就为false。
String a = "ab";
final String b = "b";
String b = "a" + b;
System.out.println(a == b); // true
字符串常量加一个final修饰的字符串常量(编译时会被解析为常量值的本地拷贝存储到常量池中),所以可以被优化 相等
String a = "ab";
final String b = getB();
String b = "a" + b;
System.out.println(a == b); // false
private static String getB()
{
return "b";
}
虽然是final修饰的但是只有在程序运行调用方法后,将返回值和“a”动态连接并分配地址赋给b,所以不相等
String str2 = new StringBuilder("java").append("技术").toString();
System.out.println(str2 == str2.intern()); //true
堆内存中有StringBuilder这个对象但是会被gc回收,toString()会new String(),这才是真正返回的引用常量池没有“java技术”这个字符串,所以intern方法返回的引用也指向s2所以为true
String str1 = new StringBuilder("ja").append("va").toString();
System.out.println(str1 == str1.intern()); //false
java作为关键字,在创建JVM的时候已经用到,存放到常量池中了,所以intern()返回的是常量池中的引用,为false
String s1=new String("test");
System.out.println(s1==s1.intern()); //false
test作为字面量已经放入了常量池中,所以intern()返回的是常量池中的引用,为false
八种基本类型的包装类和对象池
java中的基本类型大部分都实现了常量池技术(对象池,在堆上)这些类是Byte,Short,Integer,Long,Character,Boolean,宁外两种浮点型没有实现
Byte,Short,Integer,Long,Character这5种整型的包装类也只是在对应值小于等于127时大于等于-128时才可使用对象池
Integer源码
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.
}
}
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;
}
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}