Java字符串常量池

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java字符串常量池

Keane1998   2020-01-29 我要评论

字符串

  • 字符串字面量:就是指这个字符串本身,比如"Java","Hello"。
  • 字符串对象:比如new String("abc"),或者直接String s="str",后面的"str"也是一个字符串对象。
  • 字符串引用:引用就是一个变量,指向对应的字符串对象。

常量池

class常量池

Java源文件编译之后得到的class文件,其中有项信息就是常量池,保存有字面量和符号引用,比如

public class JvmClass1 {
    final int b=666;
    public static void main(String[] args) {
        String c="java";
        String d="abcd";
    }
}

对应的class文件中


这一项就是666这个字面量。


这两项就是java和abcd这两个字符串的字面量。

而符号引用也是一些常量,比如全限定类名,字段的名称和描述符,方法的名称和描述符。


这是类名。


这是变量名。

常量池中有一些常量类型有index属性,指向另外一个字面量,比如CONSTANT_Class_InfoCONSTANT_String_Info等。
相同的字符串字面量在常量池中只会保存一份,例如

public class JvmClass1 {
    public static void main(String[] args) {
        String c="java";
        String d="abcd";
        String e="java";
        String f=new String("java");
    }
}

运行时常量池 && 字符串常量池

class常量池被加载到内存后,形成了运行时常量池,Jdk1.7之前位于方法区中,Jdk1.8之后是放在元空间,或者把元空间看做是新的方法区。
运行时常量池相对于class常量池的一个特点是具有动态性,Java不要求所有常量在编译器产生,可以在运行时产生常量加入常量池,例如String类的intern()。

String.intern

intern源码的注解是Returns a canonical representation for the string object.,意思是返回字符串对象的规范表示形式。
第二段是A pool of strings, initially empty, is maintained privately by the class,说的就是字符串常量池,JDK1.6及以前是放在方法区中,后来放到了堆中,其中保存的是字符串对象的引用,而真正的字符串对象实例是在堆中创建。
第三段是

When the intern method is invoked, if the pool already contains a string equal to this {@code String} object 
as determined by the {@link #equals(Object)} method, then the string from the pool is returned. Otherwise, 
this {@code String} object is added to the pool and a reference to this {@code String} object is returned.

意思是当一个字符串对象调用intern方法,如果池中已经存在值相等(通过String的equal函数比较)的字符串常量,就返回常量池中的常量,也就是堆中对应实例的引用。否则将这个字符串加入常量池。
例如

public class JvmClass1 {
    public static void main(String[] args) {
        String a = "hello";
        String b = new String("hello");
        System.out.println(a == b);//false a和b是不同对象
        String c = "world";
        System.out.println(c.intern() == c);//true c.intern()返回的就是"world"在常量池中的引用,和c是同一个对象
        String d = new String("mike");
        System.out.println(d.intern() == d);//false d.intern()返回的类似a,而d类似b,不同对象
        String e = new String("jo") + new String("hn");
        System.out.println(e.intern() == e);//true 通过拼接得到的,并没有出现"john"的字面量,所以只有当e.intern()才加入池中,所以是同一对象
        String f = new String("ja") + new String("va");
        System.out.println(f.intern() == f);//false 有个博客说"java"在jvm启动时自动加入字符串常量池中,不过还没找到其他什么证据。
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们