File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Jim Colwood
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

following example is on page 419 of KS&BB (1.5) book...



the book says there are total of eight String objects created, PRIOR to the println statement. agreed!

1. what about " " in println? can you say that " " is created & lost instantly, assuming no String object exists in the SCP?

2. also, the answer explains which of the eight objects are "lost". are the lost objects eligible for GC?

3. is SCP part of heap? if no, how does GC work on SCP?

feel free to yell at me if these q's sound stupid :lol:
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think that what the book means by "lost" is that there is no reference in your code to those strings. However, my understanding is that they are not available for garbage collection.

When java encounters a literal string in your code like:
String s2 = s1 + "Summer";
it creates a new String object in the heap with the contents "Summer" (assuming one does not already exist) and then creates a reference in the String Constant Pool pointing to that object. That makes the object not eligible for garbage collection, because it is pointed to from a SCP reference.

The two styles of instantiating String objects are:

String s = "Text"; // 1
and
String s = new String("Text"); // 2

Case 1: Look in the SCP to see if there is a reference to "Text" (a String object that already exists in the heap with that content.) If there is one, assign that reference to s. If there is not one, create a new String object with the contents "Text" in the heap, create a new reference in the SCP referring to that object, and assign that reference to s as well (so both the SCP reference and s (the reference in your code) point to the same object in the heap.)

Case 2: Look in the SCP to see if there is a reference to "Text". If there is not one, create a new String object with the contents "Text" in the heap, and assign that object to a reference in the SCP. Additionally (this is where case 1 and case 2 differ) create another String object in the heap with the contents "Text" and assign s (your String reference) to it.

In the example that you give, the String object is "lost" in the sense that you don't have an active reference pointing to it, but it won't be garbage collected because it is pointed to from a SCP reference. Also, notice that you can refer to that very same object in your code by doing again:
String s1 = "Summer";
That is because all String literals of the same value are references to an unique String object referred to from the SCP.

There is a great discussion about this here:
http://www.coderanch.com/t/424682/Programmer-Certification-SCJP/String-literal-Pool
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, beware that there should be no questions in the SCJP 6 exam involving String garbage collection and the SCP (I think Bert has mentioned this before.) I don't know about SCJP 5 though.
 
Alpesh Rathod
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result of the above code comes a spring winter spring summer....

I m confused in the sense that the concat function doesnt maintain the state of the String object...
I mean when s1 is concatenated by fall...then its value will be "spring fall"....
Now s2.concat(s1) will print spring summer spring fall.

s1+="winter" will print "spring fall winter"..

Ok so my question is since s1 contains "spring fall winter" and s2 contains "spring summer spring fall"

Then how come the output is "spring winter spring summer"..

Help me out....
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean when s1 is concatenated by fall...then its value will be "spring fall"....


The concatenation is done on a new copy of S1(the value of S1). So s1 still equals "spring" but there is a value "Spring Fall" somewhere in the SCP but since there is no direct handler to it, that value is "lost".

There was a terrific discussion on this sometime back.

s1 = s1.concat(" fall") will give you "spring fall"
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, got the term I was missing in my last post.

String objects are immutable.
 
Alpesh Rathod
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Got it ..Thanks
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think String objects that are created at runtime is like any other normal object, not string literal, so whatever String Objects created at runtime in above lines will be eligible for GC.
 
Ronald Schild
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben Soto wrote:

*snip*

String s = "Text"; // 1
and
String s = new String("Text"); // 2

Case 1: Look in the SCP to see if there is a reference to "Text" (a String object that already exists in the heap with that content.) If there is one, assign that reference to s. If there is not one, create a new String object with the contents "Text" in the heap, create a new reference in the SCP referring to that object, and assign that reference to s as well (so both the SCP reference and s (the reference in your code) point to the same object in the heap.)

*snip*


String objects that represent string literals are created (unless a String object containing this string literal already exists) during class loading, see:

http://java.sun.com/docs/books/jls/third_edition/html/execution.html#44670

Loading of a class or interface that contains a String literal (§3.10.5) may create a new String object to represent that literal. (This might
not occur if the same String has previously been interned (§3.10.5).)


So during the execution of the code, s is only assinged to the already existing String object.

Here's what I think about the code in question:

http://www.coderanch.com/t/269601/Programmer-Certification-SCJP/KB-SCJP-topic-Important-Facts
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben is correct -

For both the SCJP 5 and SCJP 6 exams - there will be no questions that ask how the GC interacts with objects of type String.
 
yusuf Kaplan
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what about the String figures listed in the Bates&Sierra book.
For example Figure 6-3 on page 432 (Hope you guys have the version of the book like me)
The first line is On the heap is one String object. Agree
2nd line is On the heap are two objects: Java and Java Rule. But shouldnt there be also String "Rules" ???

I think the figure does not correspond with the spring, summer, lost example one page after...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic