Comments inline below: On Tue, Aug 30, 2016 at 6:38 AM, Jun Zhuang <jun.zhu...@hobsons.com> wrote:
> 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 > > > Yes, that it correct. > Question: I assume the reference returned is consumed by > the String () constructor, is it using the existing object to create a new > one? > In this specific example, yes, the reference is directly passed to the constructor call as the argument. It's the same as passing any reference type argument to any method. > > > *Step #2*: Handling the new String() > > Action: Create an instance and return reference > > > Yes. > Either way, TWO instance of String object representing Cat will end up on > the heap, right? > That is correct. The one created by the "new" expression is required to have a different identity (meaning being a different object) than the one from the "Cat" constant expression. Thus: "Cat" == "Cat" will always be true, and "Cat" == new String("Cat") will always be false. > If I have another statement String str2 = new String("Cat"); a third such > instance will be created? > > > That is correct. The "Cat" part will resolve to a reference to the same object as any earlier mentions of the interned "Cat" String instance. - Kris > 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 | jun.zhu...@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> > > > > > > [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@openjdk.java.net > http://mail.openjdk.java.net/mailman/listinfo/hotspot-gc-use > > >
_______________________________________________ hotspot-gc-use mailing list hotspot-gc-use@openjdk.java.net http://mail.openjdk.java.net/mailman/listinfo/hotspot-gc-use