Ari*_*pta 6 java string string-literals
我最近几天试图理解String常量池和inter的概念,在阅读了很多文章之后我理解了它的一些部分,但仍然对以下几点感到困惑: -
1. String a = "abc"
这会在字符串常量池中创建一个对象,但是以下代码行是否在字符串常量池中创建对象"xyz"?
String b = ("xyz").toLowerCase()
2.
String c = "qwe"
String d = c.substring(1)
d.intern()
String e = "we"
Run Code Online (Sandbox Code Playgroud)
在类加载期间是否应将字符"we"添加到String consant池中,如果是这样,为什么d==e
即使d未指向String Constant池也会返回true
字符串池正在延迟加载.如果你在字符串文字之前自己调用intern(),那么这是将进入字符串池的字符串的版本.如果你不自己调用intern(),那么字符串文字将为我们填充字符串池.
令人惊讶的是,我们可以在常量池之前影响字符串池; 正如下面的代码片段所示.
要理解为什么两个代码片段具有不同的行为,重要的是要清楚
常量池与字符串池不同.也就是说,常量池是存储在磁盘上的类文件的一部分,字符串池是用字符串填充的运行时缓存.
并且引用字符串文字并不直接引用常量池,而是根据Java语言规范jls-3.10.5 ; 当且仅当字符串池中没有值时,字符文字才会从常量池填充字符串池.
也就是说,从源文件到运行时的String对象的生命周期如下:
以下两个代码片段之间的行为差异是由于在通过字符串文字发生对内部的隐式调用之前显式调用intern()引起的.
为清楚起见,这里是对这个答案的评论中讨论的两种行为的贯彻:
String c = "qwe"; // string literal qwe goes into runtime cache
String d = c.substring(1); // runtime string "we" is created
d.intern(); // intern "we"; it has not been seen
// yet so this version goes into the cache
String e = "we"; // now we see the string literal, but
// a value is already in the cache and so
// the same instance as d is returned
// (see ref below)
System.out.println( e == d ); // returns true
Run Code Online (Sandbox Code Playgroud)
以下是在使用字符串文字后我们实习的情况:
String c = "qwe"; // string literal qwe goes into runtime cache
String d = c.substring(1); // runtime string "we" is created
String e = "we"; // now we see the string literal, this time
// a value is NOT already in the cache and so
// the string literal creates an object and
// places it into the cache
d.intern(); // has no effect - a value already exists
// in the cache, and so it will return e
System.out.println( e == d ); // returns false
System.out.println( e == d.intern() ); // returns true
System.out.println( e == d ); // still returns false
Run Code Online (Sandbox Code Playgroud)
下面是JLS的关键部分,声明实际上是为字符串文字调用实习生.
此外,字符串文字始终引用类String的相同实例.这是因为字符串文字 - 或者更常见的是作为常量表达式(第15.28节)的值的字符串 - 被"实例化"以便使用String.intern方法共享唯一实例.
JVM规范涵盖了从类文件加载的常量池的运行时表示的详细信息,并且它与实习生交互.
如果先前在类String的实例上调用了String.intern方法,该类包含与CONSTANT_String_info结构给出的Unicode代码点序列相同的Unicode代码点序列,则字符串文字派生的结果是对类String的同一实例的引用..
归档时间: |
|
查看次数: |
750 次 |
最近记录: |