• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String constant pool

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic