Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

String literals & GC  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ranchers,
Please confirm that String literals are not the subject for GC:
String s = null; //not eligible for GC
String s = new String(null); //eligible for GC
Thnx
 
Enthuware Software Support
Rancher
Posts: 4203
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>String s = null; //not eligible for GC
No. There is nothing available for GC here.
String s = "asdf"; //not eligible for GC
String object refered by s is NOT eligible for GC.
String s = new String("asdf");
String object refered by s (not s itself) is eligible for GC.
HTH,
Paul.

------------------
SCJP2, SCWCD Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
JQPlus - For SCJP2
JWebPlus - For SCWCD
JDevPlus - For SCJD
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the String constructor " new String()" then it eligible for GC when the reference is set to null. If you do it the other way(String a = "123") it is stored in a pool which is not eligile for GC. This is what i gathered from my understanding.
Good luck
Faisal
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this thread there is program that shows how string literals are not made eligable for gc:
http://www.javaranch.com/ubb/Forum24/HTML/012708.html
hope helps
 
Igor Gaschits
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all, for your prompt replies. Jose, your code killed my final doubts about this subject. Once in a while, I find this type of questions in mock exams and always choose "0 obj eligible for GC" and get failure on this question. I also hope, there will be no questions like this on the real test or at least they will accept the right choice.
Thanks again!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!