• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

referencing String objects

 
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On pg 429 in the Sierra/Bates Study Guide for Java 6 cert (diagram):


Step 1: shows the ref var "s" being stored on the heap as "abc"---got it


Step 2: indicates both ref vars, s2 and s refer to the same object on the heap ("abc")--got it


Step 3: indicates s2 refers to the "abc" object on the heap and s is a deleted ref to "abc" and "abcdef".

Step 3 is where I am running into trouble. If "def" as never assigned a ref variable, then why does it say that s is a deleted ref to "abcdef"? I am understanding the purpose of the appending of ("def") in this example is only creating the object and not assigning a reference. TIA
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at step 3 's' refers to "abcdef" and "def" is a string literal which is lost in string literal pool because none of the reference variable pointing to it.

do you have any other doubt?

 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, let me just state the way I understand this and tell me where I'm wrong:

the string literal ("def"), as it is displayed, creates an object but has no reference variable that can refer to it. In other words, how can the reference be deleted for "abcdef" if "def" reference was never created...why concatenate something for a reference variable that no longer exists?

Brings up another question:

Sooo, if "def" is not on the heap is in then in the stack as an object? and would this make it available for garbage collection if it is never referenced?


 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings live in a very special place called the String pool.

Oh, your "def" exists. It's a literal floating around in the pool somewhere. There is no reference to it, but it's swimming around in the pool. String literals in the pool are not garbage collected, even if there is no reference to them. If you create a new reference to a def:



That String in the pool gets a new reference.

HOWEVER, using the constructor actually creates a new String:

You should never do this.... allow the JVM to keep track of Strings for you.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So basically the "def" was actually concatenated with "abc", but "abcdef" is lost in the pool because the lack of a reference to it?

That I can understand..it stays in memory but not (at this point) available for use.


Sorry for hammering at this, but I really want to get this.



 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, "abcdef" won't be in the String pool, because it never was a String literal. It's also not lost, because it is referenced by s2.

"def" on the other hand, has no more references to it in your code, but it's not lost, because it's referenced by the String pool.

It will help if you understand that the String pool is no special magical place. It's simply a static list of Strings maintained by the String class. String literals in your code will be added to it automatically when your program starts.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my fault...what happens when you study way too long. I overlooked the fact that the concatenation in step 3 was indeed assigned to s. That is why I kept questioning how the concatenation could actually succeed.

Sorry for the blunder, but thanks so much for the help!
 
reply
    Bookmark Topic Watch Topic
  • New Topic