aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Jim Colwood
Greenhorn

Joined: Jul 10, 2007
Posts: 10
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

Joined: Dec 16, 2008
Posts: 1032
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


All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Jan 06, 2009
Posts: 41
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....


Thanks,
Alps
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
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

Joined: Mar 20, 2007
Posts: 317
Yeah, got the term I was missing in my last post.

String objects are immutable.
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
Yeah Got it ..Thanks
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


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.


SCJP 6
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
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

Java hobbyist.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
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.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 58
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings