This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Again on GC( JQ++) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Again on GC( JQ++)" Watch "Again on GC( JQ++)" New topic

Again on GC( JQ++)

swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Please help!!!
How many objects are eligible for garbage collection when the control reaches line 4 if the method process() is called with the argument of 5?

1. public void process(int count)
2. for ( int i = 1; i < count; i++ )
3. Object temp = " Hello "+i;

Ans is 0 as--
All the String objects are interned ie. Java caches all the string objects (except which are created by doing new)

Is this because in the above code " Hello "+i
is creating a string literal object (not a string object on heap )using constant expression so that's why the Garbage collection doesnot take place???
And if the statement would have been like
String str=new String("Hello");
Object o=str+i;
then would the answer be 4??

Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
The following posts discuss the same question:

[Blog] [Blogroll] [My Reviews] My Linked In
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
thanks Valentin
Whatever i could get from those discussions is
that temp is not a String literal as in the concatenation there is one String literal and other is a variable whose value is not known at
compile time so this is computed at runtime thus
making the temp as a String object.
Am i right now??? let me know..
I agree. Here's the link:
subject: Again on GC( JQ++)
It's not a secret anymore!