java常量池

时间:2020-05-20 14:28:36   收藏:0   阅读:52

一、概述

常量池大体可分为两类:

二、常量池的好处

常量池是为了避免频繁的创建和销毁对象而影响系统性能,其实现了对象的共享

例如字符串常量池,在编译阶段就把所有的字符串文字放到一个常量池中。

三、字符串常量池

两个方法使用常量池:

String s1 = "Hello";
String s2 = "Hello";
String s3 = "Hel" + "lo";
String s4 = "Hel" + new String("lo");
String s5 = new String("Hello");
String s6 = s5.intern();
String s7 = "Hel";
String s8 = "lo";

System.out.println(s1 == s2);  // true
// 新建String对象的时候,如果直接使用字符串字面量赋值,那么字面量会直接放入class文件的常量池中。

System.out.println(s1 == s3);  // true
// Hotspot中编译时"Hel" + "lo"将直接变成"Hello"

System.out.println(s1 == s4);  // false
// new String("lo")这部分不是已知字面量,是一个不可预料的部分,编译器不会优化,
// 必须等到运行时才可以确定结果

System.out.println(s7+s8 == s1);  // false
// 相当于new String(s7 + s8),存放在堆中

System.out.println(s4 == s5);  // false
// 都在堆中,指向不同地址

System.out.println(s1 == s6);  // true
// intern方法,首先在常量池中查找是否存在一份字面量相等的字符串,如果有的话就返回该字符串的引用,
// 没有的话就将它加入到字符串常量池中,并返回在常量池中的地址

注:在java 中,直接使用==操作符,比较的是两个字符串的引用地址,并不是比较内容,比较内容为String.equals()。

解析:

String s = new String("abc"); // 创建了几个对象

String s = "abc";	// 创建了几个对象

第一句:

第二句:

四、包装类的常量池(缓存)

自动装箱就是valueOf这个方法,自动拆箱就是intValue方法。

除了包装类Long,Double,Float 没有实现这个缓存技术,其它的包装类均实现了它。

当我们给Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf,下面是valueOf的源码

@HotSpotIntrinsicCandidate
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 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() {}
}

可以发现,如果使用Integer e = 1 或 Integer e = Integer.valueOf(1),并且整型字面量在-128 ~127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,可以当做基本类型比较。

Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;

System.out.println(f1 == f2);	// true
System.out.println(f3 == f4);	// false

但是,如果使用Integer e = new Integer(1),无论值是多少,都要作为对象比较。

Integer n1 = new Integer(47);
Integer n1 = new Integer(47);

System.out.println(n1 == n2);	// false
System.out.println(n1 != n2);	// true
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!