wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Again on GC( JQ++)" Watch "Again on GC( JQ++)" New topic
Author

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;
}
4.
}

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??
swapna


SCPJ2
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The following posts discuss the same question:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014870
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014957
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014328


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[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..
swapna.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Again on GC( JQ++)
 
Similar Threads
A question on GC and String literals.
Garbage for objects and strings ...
GC question from JQ+
Garbage Collection of Objects in a for loop
Question on GC from JQPlus