• 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 literals immune from GC, correct?

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literals are always immune from the GC, right?
So if asked a question about when the following object is available for GC:
String str = "Java Ranch";
str = null;
The answer is never??
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example to show (but not prove) that string literals and interned string objects are not garbage collected.

Output:
string-1,string-2,string-3
string-1,string-2,string-3
string-1,string-2,null
Jose, did I do this correctly? Thank you for the idea.
[ June 25, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The same constant is loaded. Does that suggest that the same String object is being referenced?
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marlene Miller:

The same constant is loaded. Does that suggest that the same String object is being referenced?


Hi Marlene,
Yes, I think its the same 'Object'.
From the book "Inside the Java Virtual Machine by Bill Venners"


Three opcodes (ldc,ldc_w, ldc2_w) push constants from the constant pool. These opcodes take operands that specify a constant pool index. The Java virtual machine looks up the constant pool entry given the index, determines the constant's type and value, and pushes it onto the stack.

 
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 Brian Joseph:
String literals are always immune from the GC, right?


Right


So if asked a question about when the following object is available for GC:
String str = "Java Ranch";
str = null;
The answer is never??


You should never be asked a question like this (at least for the SCJP exam) because garbage collection of String literals IS NOT covered on the exam.
Corey
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the replies. And thanks for the clarification, Corey. I just assumed garabage collection meant that. I downloaded a 1.4 mock exam program, but after taking it and being asked such a question I had no way to verify it because the darn thing didn't show me what I got wrong! It really was like the real thing :-)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WARNINGthe following is not exam stuffWARNING
Hey Marlene I am sure you would appreciate to know that string literals can be g.c.ed as this post shows.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Jose - in advance of reading your reference. I scanned the forum (104 search results), but I missed that one. Yes, I am interested.
[ June 26, 2003: Message edited by: Marlene Miller ]
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's great to have the experienced ranchers point out when a topic is not covered on the exam.
Since there "seems" to be an infinite number of details you can learn if you want, there comes a point where you really only want spend your time learning topics which Sun considers most relevant (thus are on the exam).
Thanks!
[ June 26, 2003: Message edited by: Brian Joseph ]
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic