The moose likes Java in General and the fly likes Will a new String( Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Will a new String("something") reference placed in literal pool?" Watch "Will a new String("something") reference placed in literal pool?" New topic
Author

Will a new String("something") reference placed in literal pool?

Nithya Vasudevan
Greenhorn

Joined: Feb 10, 2012
Posts: 2
Hello

I went through this documentation on Strings http://www.javaranch.com/journal/200409/Journal200409.jsp and I would like to know what happens when there is a statement like this,



Now, will the string literal pool contain a reference to string "something" object in heap ?

Thanks
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 1610

Nithya Vasudevan wrote:Now, will the string literal pool contain a reference to string "something" object in heap ?

Yes.

Winston


More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
To elaborate on what Winston wrote. Yes. And no.
To the question in your post, yes. To the question in the thread title, no. Which just shows how careful you have to be about what you ask. Try adding this line
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3143
Nithya Vasudevan wrote:Hello

I went through this documentation on Strings http://www.javaranch.com/journal/200409/Journal200409.jsp and I would like to know what happens when there is a statement like this,



Now, will the string literal pool contain a reference to string "something" object in heap ?


Before you even get to that line of code, the pool has the "something" literal in it. It's put there when the class is loaded, if there wasn't already a "something" String there.

Then, when you get to that line, a new String object is created, and the constructor invoked is the one that takes a reference to an existing String that will be copied. We pass it a reference to the "something" String in the pool, that String object is copied, and we now have two "something" String objects, one in the pool, and one not.
Dennis Deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 547
In your example, a String literal will be placed in the pool, but only because the literal is passed into the constructor. Be aware that we can use new String without a literal.

Consider this:

Here, neither String is placed in the literal pool.


OCPJP 6
Nithya Vasudevan
Greenhorn

Joined: Feb 10, 2012
Posts: 2
Thanks all for clarifying.
 
 
subject: Will a new String("something") reference placed in literal pool?
 
Threads others viewed
help!!
Strings and String pools ...confusing at times...
== operator
Journal Article - The SCJP Tip Line - Strings, Literally
How we can find that how much string objects has been created?
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture