• 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

Garbage collect from Sarga.com Exam

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: String anObj = "sample";
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
11: anObj = null;
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 12
The answer given is d. But I marked 'e'. Could anyone pls. explain me which is correct.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcourse answer is d. Coz after line 9, anObj refeers to a new object which is anObj.toUpperCase(). Remember string objects are immutable, u can't change anything in object. It seems u r changing the same object to uppercase by in reality new string object is returned by method toUpperCase().
Now think the scinerio:
1.Create anObj as StringBuffer.
2.do charAt() as convert each char to upper case.
3.Now u will be modifying teh same object.
right?
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi hemanshow,
I do not agree with your answer because before line#9 anObj refer to a String object created on 'String Pool'. we all know that string object cretaed on 'String pool' are not available for GC.
so when we create a new object on line#9 it is being created on heap. so it is available for GC after line#11.
java expert please do verify it. please correct me if am wrong.
waiting for reply.
vivek
[This message has been edited by Vivek Shrivastava (edited July 20, 2000).]
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Looking forward to hear from our java expert.
regards
vivek
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I wouldn't really qualify myself as a java expert,
anyways coming to the qstn here I go...
I have seen in the previous post (abt 4 mths back) it was
advised on the ranch that for the purpose of SCJP exam,
we should forget abt the concept of String pools.
Once we forget that, the other concept is (my first lesson
from Maha) "anObj" is a reference to an object and the
object is what gets GC'd.
Thought the qstn is ambiguous (which obj is to be GC'd?
the obj sample or the uppercase obj SAMPLE)
in such cases, assuming the author refers to the obj
sample the answer would be d.
Why the assumption, i have no idea...I told you my ambiguity
abt the qstn before....
Hopefully we agree.....
Regds.
- satya
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic