• 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 & Garbage collector

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am preparing for my java certification exams..I had a couple of questions that I was hoping someone would help me out with. 1. Can Garbage collection be forced?
Simon Roberts book says No, Khalid Mughal says Yes.....I
would appreciate it if someone can help me find the correct
answere to this question.
2. String is an object type & not a primitive data type - so we
can create a new String object by referencing it with the new
keyword
(ex: String s = new String("need help!") .
So why does some of the examples simply create a new String
object w/o using the new keyword
(ex: String s = ("need help!")

------------------
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Garbage collection can not be forced , I know about Khalid's book and don't know why he wrote like this..
strings can be created both ways ..RHE has explained it in a very detailed maner.
RHE--->Java -2 Certification study guide
Wali
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A <CODE>String</CODE> created with the <CODE>new</CODE> operator is treated a little different from a literal creation of a String as in <CODE>String s = "Hello";</CODE>.
The first one may be garbage collected when there are no more references to it while the second one goes into what is called the "String pool" and can be denoted by many references to it. I don't know the details of GC in relation to the String pool, but since this is a Certification forum, it doesn't matter since the details of GC are not included in the exam. Only the general "truths" such that it cannot be forced, runs unpredictably (for the programmer) and it executes any finalize() method implemented by the object.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks WaliM for suggesting that guide - I'll definitely go thru' it!

Originally posted by WaliM:
Hi
Garbage collection can not be forced , I know about Khalid's book and don't know why he wrote like this..
strings can be created both ways ..RHE has explained it in a very detailed maner.
RHE--->Java -2 Certification study guide
Wali


 
Chamya Pehalvan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony..as I also saw in the String class @ the constructor.. I look forward to all the help I can get from you guys!

Originally posted by Tony Alicea:
A <CODE>String</CODE> created with the <CODE>new</CODE> operator is treated a little different from a literal creation of a String as in <CODE>String s = "Hello";</CODE>.
The first one may be garbage collected when there are no more references to it while the second one goes into what is called the "String pool" and can be denoted by many references to it. I don't know the details of GC in relation to the String pool, but since this is a [b]Certification
forum, it doesn't matter since the details of GC are not included in the exam. Only the general "truths" such that it cannot be forced, runs unpredictably (for the programmer) and it executes any finalize() method implemented by the object.[/B]


 
reply
    Bookmark Topic Watch Topic
  • New Topic