This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

how many objects are eligible for garbage collection?  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the following code how many objects are eligible for garbage collection?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Test" is the only object that is not being referenced after execution. Both are currently pointing to "Today". So my answer would be: A)1
Percy
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is 0.
String literals are not garbage collected.
However if the code was written like this....
String string1 = new String("Test");
String string2 = new String("Today");
string1 = null;
string1 = string2;
The answer would be 1.
 
Percy Densmore
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification, Jimmy. I knew better

------------------
Percy Densmore
-SCJP2 Die Hard Student
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Percy Densmore you answer is correct. "test" has not be reffernce after the first execution, therefore it is eligible for garbage collection. a. 1 "is the answer"
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to agree with Jimmy.
The J2SE API spec for the String.intern() method states "A pool of strings, initially empty, is maintained privately by the class String." Additionally, "All literal strings and string-valued constant expressions are interned," i.e. they are added to the String pool.
This implies that the String class manages "Test" and "Today" within its private String literal pool. Hence, even if "Test" is no longer referenced, the pool manager may still be holding on to it, thereby preventing the garbage collector from freeing the object from memory.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when strings are created with the = operator, they are only stored in the string literal pool. strings in the string literal pool are not eligible for garbage collection
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jimmy: String literals are not garbage collected.
Garbage collection questions involving Strings will not be included on the exam.
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jimmy.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This appears to be a question rather than an indication of errata in some unspecified mock exam. I will move it to the Programmer Certification Study forum.
 
I just had the craziest dream. This tiny ad was in it.
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!