Hi Kris,
Thanks for the response. Regarding the 2nd question, I want to make sure I
understand your answer. The handling of new String("Cat") can be broken into
two steps:
Step #1: Handling the “Cat” expression
If ( Does not already exist )
Create an instance on the heap
Add a reference to the StringTable for the above instance and return the
reference
else
Return the reference from the StringTable
Question: I assume the reference returned is consumed by the
String () constructor, is it using the existing object to create a new one?
Step #2: Handling the new String()
Action: Create an instance and return reference
Either way, TWO instance of String object representing Cat will end up on the
heap, right? If I have another statement String str2 = new String("Cat"); a
third such instance will be created?
Thanks much,
Jun
--------------------------------------------------------------------------------------------------------------------------------------------
#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<tel:%2B1%20513%20746%202288> |
[email protected]<mailto:[email protected]>
50 E-Business Way, Suite 300 | Cincinnati, OH 45241 | USA
[Upgraded by Hobsons - Subscribe
Today]<https://geo.itunes.apple.com/us/podcast/upgraded/id1086217543?mt=2>
Upgraded by Hobsons - Subscribe Today
_______________________________________________
hotspot-gc-use mailing list
[email protected]<mailto:[email protected]>
http://mail.openjdk.java.net/mailman/listinfo/hotspot-gc-use
_______________________________________________
hotspot-gc-use mailing list
[email protected]
http://mail.openjdk.java.net/mailman/listinfo/hotspot-gc-use