aspose file tools*
The moose likes Beginning Java and the fly likes String constant pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String constant pool" Watch "String constant pool" New topic
Author

String constant pool

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

If I create a String object as follows:



My understanding of what is happening in the background is as follows:

a) Two new String objects are created. A new String object "Hi" is created on the heap and is being referenced by "a". Another "Hi" is in the pool. Correct?

b) When I say , b starts pointing to "Hi" String object on the heap.

c) Also, if I say, , it will directly be created in the pool and c refers to it. Why is this so? Why this peculiar difference between behavior of creating a String literal and a String object using "new" keyword?

d) What happens if I then say ? Will that "Bye" object still be there in the pool? If yes, would it be up for GC by JVM?




~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Mansukhdeep Thind wrote:Hi

If I create a String object as follows:



My understanding of what is happening in the background is as follows:

a) Two new String objects are created. A new String object "Hi" is created on the heap and is being referenced by "a". Another "Hi" is in the pool. Correct?
Yes. The new operator and copy constructor create a second String
b) When I say , b starts pointing to "Hi" String object on the heap.

c) Also, if I say, , it will directly be created in the pool and c refers to it.
The literal "Bye" is a String object in its own right, and that is added to the String constant pool
d) What happens if I then say ? Will that "Bye" object still be there in the pool? If yes, would it be up for GC by JVM?
As far as I remember, that remains in the String constant pool as long as any classes whose code includes that String literal or another identical String literal remain loaded.
Search this forum for “Strings, literally”, and you find an old JavaRanch Journal article by that name which should explain everything.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:
Mansukhdeep Thind wrote:Hi

If I create a String object as follows:



My understanding of what is happening in the background is as follows:

a) Two new String objects are created. A new String object "Hi" is created on the heap and is being referenced by "a". Another "Hi" is in the pool. Correct?
Yes. The new operator and copy constructor create a second String
b) When I say , b starts pointing to "Hi" String object on the heap.

c) Also, if I say, , it will directly be created in the pool and c refers to it.
The literal "Bye" is a String object in its own right, and that is added to the String constant pool
d) What happens if I then say ? Will that "Bye" object still be there in the pool? If yes, would it be up for GC by JVM?
As far as I remember, that remains in the String constant pool as long as any classes whose code includes that String literal or another identical String literal remain loaded.
Search this forum for “Strings, literally”, and you find an old JavaRanch Journal article by that name which should explain everything.



What is meant by copy constructor?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

just repeating for clarity -->
new String("Hi"); "Hi" created at the time of class loading and another object is created(copy of "Hi") when this line reach in execution new String("Hi");

Copy_constructor (<= click)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Mansukhdeep Thind wrote: . . . What is meant by copy constructor?
You would appear to have worked that out. You will remember it a lot longer because I didn’t tell you.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Got it . It is a constructor that creates an object similar to original. We use to alter state of the copy rather than the original. HIGH 5!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String constant pool