java8中的常量及常量池

540 阅读6分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第9天,点击查看活动详情

1.常量

常量表示程序运行过程种不可改变的值,主要作用如下: 1.代表常数,便于程序的重构和修改。 2.增加程序的可读性。 在java中,常量的语法格式只需要在变量前添加final即可。代码规范要求常量名称须用大写字母。 如:

final String USERNAME = "test";

也可以首先申明,再进行赋值,但是只能赋值一次:

final String USERNAME;
USERNAME = "test";

2.Java常量池

在java中,为了避免频繁的创建和销毁对象影响系统的性能,引入了常量池,通过常量池实现了对象的共享。 通过常量池,从而实现了以下好处: 1.节省内存空间,常量池中所有的相同对象会被合并,只占一个对象的空间。 2.节省运行时间,通过==对字符串比较,速度比equals()快。(在基本数据类型中,==比较的是数值;在复合数据类型中,比较的是内存地址。) java的常量池可做如下分类:

2.1. 静态常量池:

即*.class文件中的常量池,class文件中的常量池不仅仅包含字符串(数字)字面量,还包含类、方法的信息,占用class文件绝大部分空间。静态常量池主要用于存放两大类常量:字面量(Literal)和符号引用量(Symbolic References),字面量相当于Java语言层面常量的概念,如文本字符串,声明为final的常量值等,符号引用则属于编译原理方面的概念,包括了如下三种类型的常量,类和接口的全限定名、字段名称和描述符、方法名称和描述符。

2.2. 运行时常量池:

是jvm虚拟机在完成类装载操作后,将class文件中的常量池载入到内存中,并保存在方法区中,我们常说的常量池,就是指方法区中的运行时常量池。 运行时常量池是方法区的一部分。在jvm1.8中,则是metaspace的一部分。 CLass文件中除了有类的版本、字段、方法、接口等描述信息外,还有一项信息是常量池,用于存放编译期生成的各种字面量和符号引用,这部分内容将在类加载后进入方法区的运行时常量池中存放。 运行时常量池相对于CLass文件常量池的另外一个重要特征是具备动态性,Java语言并不要求常量一定只有编译期才能产生,也就是并非预置入CLass文件中常量池的内容才能进入方法区运行时常量池,运行期间也可能将新的常量放入池中,这种特性被开发人员利用比较多的就是String类的intern()方法。

3.基本类型的包装类常量池

在java中,基本数据类型byte、char、int 、short、long、boolean的包装类 Byte、Character、Integer、Short、Long、Boolean都实现了常量池。

3.1 IntegerCache

IntegerCache的源码位于Intger包中。

/**
 * Cache to support the object identity semantics of autoboxing for values between
 * -128 and 127 (inclusive) as required by JLS.
 *
 * The cache is initialized on first usage.  The size of the cache
 * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 * During VM initialization, java.lang.Integer.IntegerCache.high property
 * may be set and saved in the private system properties in the
 * sun.misc.VM class.
 */

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.
            }
        }
        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类的内部类。这段注释非常关键。意思是说,IntegerCache对-128-127之间的数据自动装箱支持。在IntegerCache第一次使用的时候通过static的构造方法进行初始化。可以通过-XX:AutoBoxCacheMax=设置IntegerCache缓存的大小。java.lang.Integer.IntegerCache.high可以指定IntegerCache支持的最大值。

3.2 LongCache

LongCache没有IntegerCache这么复杂,不能通过参数对LongCache进行修改。

private static class LongCache {
    private LongCache(){}

    static final Long cache[] = new Long[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Long(i - 128);
    }
}

作为Long包装类的内部类,从初始化开始,就实现了[-128 - 127]之间的范围。

ShortCache

ShortCache与LongCache类似,作为内部类:

private static class ShortCache {
    private ShortCache(){}

    static final Short cache[] = new Short[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Short((short)(i - 128));
    }
}

也是在初始化的时候就定义了这个缓存。

3.3 ByteCache

ByteCache也与之类似:

private static class ByteCache {
    private ByteCache(){}

    static final Byte cache[] = new Byte[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Byte((byte)(i - 128));
    }
}

在初始化的过程中就定义了[-128 - 127]这个范围的缓存。同样也不支持任何方式的调整。

3.4 CharacterCache

而对于Character支持的范围就变成了0-127.

private static class CharacterCache {
    private CharacterCache(){}

    static final Character cache[] = new Character[127 + 1];

    static {
        for (int i = 0; i < cache.length; i++)
            cache[i] = new Character((char)i);
    }
}

3.5 BooleanCache

boolean是一种特殊情况,没有采用上面这种内部类的方式。由于boolean只有两个结果。那么在Boolean类内部 :

public static final Boolean TRUE = new Boolean(true);
 
public static final Boolean FALSE = new Boolean(false);

定义了两个表示true和false的内部类。这样在比较的过程中,所有的Boolean实际上所有boxed的对象最终都只有这两个类。

3.6 对比

对于java中的8种基本的数据类型,存在Cache的情况如下:

原始类型缓存类型范围是否可调整
intIntegerCache-128-127通过-XX:AutoBoxCacheMax=可以设置,改变上限,下限不可调整
longLongCache-128-127不可调整
shortShortCache-128-127不可调整
byteByteCache-128-127不可调整
charCharacterCache0-127不可调整
boolean内部类TRUE/FALSEtrue/false不可调整
float
double

java在装箱和拆箱的时候,都会使用其valueof方法。来强制让这些操作走缓存。这样可以节省大量的内存空间。需要注意的是:

  • GC对常量池cache缓存没有任何影响。除非类从jvm卸载。
  • -XX:AutoBoxCacheMax= 参数设置对JVM client模式无效,只对sever模式有效。

在系统中的数字在一个较小的范围内变化的时候,可以通过缓存的方式,提前创建好。

4.java字符串常量池

在java中,用String表示字符串。查看String的的源码,可以发现,Stirng都是由char数组构成的产量。

 private final char value[];

jvm为了避免重复创建过多的String对象,因此也会用常量池就行优化。在java代码中,所有代码中的字符串会被加入常量池。所有new的对象,会重新在堆内存中分配空间。String可以通过如下两种方式进行创建。

  String str1 = "abcd";//直接在常量池中得到对象
  String str2 = new String("abcd");//在堆中创建对象
  System.out.println(str1==str2);//false

需要注意如下几点: 1.jvm在编译的过程中,会对java代码进行优化,对于String字符串,凡是使用+号而又没有其他变量参与的表达式,都会合并为一个字符串并写入常量池。 2.对于所有包含new方式新建的对象(保含null)的+号连接的表达式,所产生的新对象不会放入常量池。 2.intern 方法可以将堆中的对象设置到常量池。 参考如下demo

String s1 = "hello";
String s2 = "hel" + "lo";
String s3 = "hel";
String s4 = s3+"lo";
String s5 = new String("hello");
String s6 = "hello"+null;
String s7 = new String("hel");
String s8 = s7 + "lo";
System.out.println(s1 == s2); // true 带+表达是被jvm优化合并为一个,常量池比较
System.out.println(s1 == s4);//false 表达式有变量,无法优化,新值在堆中
System.out.println(s1 == s5);//false s5是堆中 s1在常量池
System.out.println(s1 == s6);//false 表达式有null无法优化 其结果在堆中重新分配
System.out.println(s1 == s8);//false 堆中重新分配
String s9 = new String("hello").intern();
System.out.println(s1 == s9);//通过intern重新写入常量池