• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQPLUS question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
}
The ans for this is 0. But i feel it shld be 4 coz each time the loop is run the following strings will be created i.e Hello0,Hello1,Hello2,Hello3 which are distinct and will be eligible for garbage collection.
Can anyone clarify???
Thanks..Chris.
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. All should be eligible for GC. But not because they are distinct but because they are created dynamically at runtime. Doing " Hello "+i does not allow compiler to assume them as string literals because i is a variable. If it were something like " hello" +" world", it would become a new string literal " hello world" at compile time and would not GCed.
HTH,
Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
[This message has been edited by Paul Anil (edited August 08, 2001).]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi anil
you may be write but my concept is still not clear, As you said that
"Hello" + 1 will not be acted as object then what is that suppose if we assume the "Hello" is in String pool and variables are concating dynamically. then according to you
String s = "Hello" + 1;
String s1 = "Hello" + 2;
System.out.println(s==s1);
should print true because they are in the String Pool and no object is generated.
The big problem is why "Hello" + 1 ---> "Hello1". why this is not acted as a String object
thanks in Advance
Naveen
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naveen,
String s = "Hello" + 1 will create a String object "Hello1"
String s1 = "Hello" + 2 will create a String object "Hello2"
They are two different String objects so (s == s1) will return false; won't matter if they are in the String pool or not.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic