• 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

Intern strings eligible for garbage collection?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During my preparation for the SCJP exam I have come across a type of question for which I don't agree with the given answers.
The question shows a code extract (not an entire program) where several strings are created/accessed in the string pool. Then, it asks how many objects are eligible for garbage collection after line xy.
But I thought that strings from the string pool are only eligible when the JVM exists
Here's an example from MindQ's exam:
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
Answer is 1 object eligible for garbage collection ???
Thank you to anybody who might push me in the correct direction
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no strings created when your code runs; ie, no String objects get instantiated.
The strings in the code are compile time constants. At compile time, an entry is added to the class's constant table for each of these string constants. When your class file gets loaded, three new strings are created in the string pool, and a reference to each is saved in the class's constant table. So, as long as the class is loaded in the JVM, those interned strings will always have references to them (via the constant table) and thus will never be garbage collected.
 
Pierre Post
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response. That is what I have expected.
But to return to the mentioned example, wouldn't the correct answer then be 0 (i.e. no string is eligible for garbage collection)? Otherwise, if we suppose the JVM exits after the few lines of code, the answer would be 3, no?
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
as i've seen here I came to the conclusion that there is as much string pool as classes loaded.
For example if you load 3 classes there is 3 String pool. Am I wrong? Does every classe hold it's own table (string pool)?
I suppose that I'm wrong since the JLS specify :"Literal strings within different classes in the same package represent references to the same String object. " and "Literal strings within different classes in different packages likewise (what does this likewise mean?) represent references to the same String object."(�3.10.5)
I know that I miss something but don't know what.
Can you help me?
[ June 18, 2002: Message edited by: Younes Essouabni ]
 
Pierre Post
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, after several discussions I have come to the conclusion that the correct answer would be 0 or 3, but impossibly 1. Never trust 100% the answers of mock exams ;-)
Thank you all for your help!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Younes Essouabni:
...likewise (what does this likewise mean?)...


Likewise means "similarly." All this is saying is that all classes (no matter which package they are in) refer to the exact same String if they use the same String literal. They all "share" a set of String constants. Each class does not have its own.
As far as the original question is concerned. The answer is 0. String literals are never garbage collected.
Corey
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you explain a little more this "similarly". :roll:
Does it mean differents package being parts of the same application? Thx for the explanation!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Younes Essouabni:
Could you explain a little more this "similarly". :roll:
Does it mean differents package being parts of the same application? Thx for the explanation!


likewise
similarly
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for your help!
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from JLS 3.10.5 :


Literal strings within different classes in different packages likewise represent references to the same String object.

 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx Jose, it's exactly what I wrote a little bit up.
I read your discussion with Rob and it was really interessant.
Thx a lot! Ths for sharing your thoughts.
[ June 19, 2002: Message edited by: Younes Essouabni ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic