Java string literal pool

Krystal Mok rednaxelafx at gmail.com
Mon Aug 29 23:28:19 UTC 2016


Hi Jun,

Comments below inline:

On Mon, Aug 29, 2016 at 6:29 AM, Jun Zhuang <jun.zhuang at hobsons.com> wrote:

> Hi,
>
>
>
> I was reading about the Java8 Metaspace the other day, then got into the
> topic of string pool. I got conflicting information regarding a couple of
> things, I wonder if I can get a definitive answer from the experts?
>
>
>
> #1: Is the string literal/constant pool a heap area that holds actual
> string literal objects or a place that holds references to string literal
> objects?
>
> Reference: http://stackoverflow.com/questions/11700320/is-string-
> literal-pool-a-collection-of-references-to-the-string-object-or-a-col
>
>
>

What you're referring to is called the "StringTable" in HotSpot JVM [1].

You can think of it as being semantically equivalent to a HashMap<String>
in Java, which holds references to the java.lang.String objects being
interned, instead of storing the actual string contents.


> #2: Is the following statement correct?
>
>
>
> String str = new String("Cat");
>
> In above statement, either 1 or 2 string will be created. If there is
> already a string literal “Cat” in the pool, then only one string “str” will
> be created in the pool. If there is no string literal “Cat” in the pool,
> then it will be first created in the pool and then in the heap space, so
> total 2 string objects will be created.
>
>
>
> From http://www.journaldev.com/797/what-is-java-string-pool#comment-36152
>
>
>

For this statement, yes, either 1 or 2 java.lang.String instances are going
to be created. But I wouldn't frame it that way, since it's potentially
confusing.

The correct way to frame this is to make a clear distinction between
one-time actions and normal runtime actions.

The "Cat" expression is a reference to a compile-time constant of type
java.lang.String. At runtime, there will be a one-time resolution for such
a reference. Indeed, such resolution will probe the StringTable to see if a
matching String instance is already referenced by the StringTable, if so
return that reference; otherwise create a java.lang.String instance from
the Symbol representing "Cat", intern the reference into the StringTable,
and return that reference.

The "new String(...)" expression, on the other hand, is a "new" expression.
Semantically it should always create a new String instance every time.

- Kris

[1]:
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/312e113bc3ed/src/share/vm/classfile/symbolTable.hpp#l255


>
>
> Apprciate your answers,
>
> Jun
>
>
>
> *Jun Zhuang*
>
> *Sr. Performance QA Engineer* | Hobsons
> <https://www.hobsons.com/?utm_source=outlook&utm_medium=email&utm_campaign=banner_02.12.16_general>
>
> T: +1 513 746 2288 | jun.zhuang at hobsons.com
>
> 50 E-Business Way, Suite 300 | Cincinnati, OH 45241 | USA
>
>
>
> [image: Upgraded by Hobsons - Subscribe Today]
> <https://geo.itunes.apple.com/us/podcast/upgraded/id1086217543?mt=2>
>
> _______________________________________________
> hotspot-gc-use mailing list
> hotspot-gc-use at openjdk.java.net
> http://mail.openjdk.java.net/mailman/listinfo/hotspot-gc-use
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-gc-use/attachments/20160829/6259859f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image550000.png
Type: image/png
Size: 13602 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/hotspot-gc-use/attachments/20160829/6259859f/image550000.png>


More information about the hotspot-gc-use mailing list