File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from JQ+ 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 "question from JQ+" Watch "question from JQ+" New topic

question from JQ+

Padma Shyam

Joined: Feb 14, 2002
Posts: 7
1. public void process(int count)
2. for ( int i = 1; i < count; i++ )
3. Object temp = " Hello "+i;
The question is that:
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?
The answer given is 0.Can anybody explain this?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I believe the answer lies in the way String literals are handled in Java. I may need a little help on this one, but I'll try to explain it as best I can.
Look at the following code:

In line 1, a String literal is created. These literals are not, generally, garbage collected. If another String reference is created and initialized with the same literal (as in line 3), both variables will reference the exact same String literal. This optimization can be done because Strings are iummutable in Java. Even if r and t and both assigned null (as in line 4), if a new String is created with the same String literal (line 5), it will reference the same String object that was created initially for r. These String literals don't normally go away. If memory is getting tight, I think they will be garbage collected, but they don't normally become eligible.
However, if you create a String like s, as in line 2, it behaves like any other object. None of the other Strings (r or t) reference the same String object that s references. And, like other objects, when no references to it exist (as is the case after line 4), it becomes eligible for garbage collection.
So, since String literals are being used to create Strings, rather than the keyword new, no objects are eligible for garbage collection after that loop executes.
If you'd like to see some more evidence of this behavior, try the following code:

I hope that helps,

SCJP Tipline, etc.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I don't agree with that - new Strings are being created because of the expression.
"Hello " + i
Exactly how many are GC-able depends on how the JVM treats the temp reference after the scope of the loop has been left but before the method returns. See this for a discussion of a similar question.
This just goes to show why using String objects for discussion of GC is a bad idea.
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
The number of objects eligible for gc after the loop ends will be the number created during the loop execution.
In this case, the character sequence " Hello " is a string literal, and will not be garbage collected.
However, the expression " Hello " +i has to be evaluated at runtime. The string concatenation operator causes an implicit call to a StringBuffer.append() to actually create the new concatenated string.
The new string is definately NOT a string literal. It's just a regular string as if you had written new String(" Hello " + i);
This string, being NOT a string literal, is eligible for garbage collection when there are no more references to it. This occurs in the example as soon as the for-loop block exits.
[ February 18, 2002: Message edited by: Rob Ross ]

SCJP 1.4
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
I agree with Bill and Rob. This question should be removed.

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
check this

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
I agree. Here's the link:
subject: question from JQ+
It's not a secret anymore!