Help coderanch get a
new server
by contributing to the fundraiser
  • 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

String literal

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jtips Mock Exam 2, Q42
class Test {
static boolean status;

public static void Process(boolean status) {
if(!status) {
String s = "Java";
int i;
s = "JavaScript";
String x = "Java";
String y = x;
x=null;
i=1;
};
}
public static void main(String [] args) {
Test t = new Test();
Process(status);
}
}
How many objects are eligible for Garbage Collection?
Given answer is 2. Is it right?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer should be 0. The only objects are the Strings and since they aren't created with the new operator, they should remain in the string pool.
Is there a good way to test these kinds of GC questions?
 
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 Jim,
You're right in stating that String literals are not gc'd. Unfortunately many of the mocks ignore this fact and use String literals in their gc questions.
When you are doing mocks assume String literals are gc'd. The real exam gc questions use non-String objects.
In this question 2 string objects are created in the Process method; both will be eligible for gc when the method completes as no references to them will exist when control is passed back to main().
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport
[This message has been edited by Jane Griscti (edited December 04, 2001).]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point of the question is that the original String s and String x are eligible for garbage collection. I have always disliked these questions, because they don't give an exact point of where the GC judgement is made.
Upon completion of the Process method, all local variables are eligible for GC. Upon completion of the program, all variables in the program are eligible for GC. The SCJP will be much clearer; it will specify a line at which to evaluate.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String objects that represent String literals are not made eligable for G.C. when the method reurns because they are still pointed to by the String pool.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:
Hi Jim,
You're right in stating that String literals are not gc'd. Unfortunately many of the mocks ignore this fact and use String literals in their gc questions.
[b]When you are doing mocks assume String literals are gc'd
. The real exam gc questions use non-String objects.
In this question 2 string objects are created in the Process method; both will be eligible for gc when the method completes as no references to them will exist when control is passed back to main().
Hope that helps.
[/B]


hi ,
i would like to ask a doubt( it may not be relevant).
can we declare
String s ="java".
I mean String is class and do we not have to use the new keyword. kindly explain.Similarly with String x="Java".
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first time the JVM uses the variable pointing to a String literal (roughly speaking) it creates a String object that is added to the String pool. There is only a copy of a given String object in this pool. And objects pointed by it are not made g.c. eligable.
If the string to create is already in the pool it returns the address of this one in the pool (avoiding the creation?).
String.intern() does exactly the same for "normal" Strings.
 
I've never won anything before. Not even a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic