• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

urgent pls hlp !!!

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
question regarding GC,
String name;
String newname="nick";
newname="jason";
name="fred";

String newsname=name;

name=null;
//line a

how many objects are eligible for garbage collection once execution has reached the line a the mock exam says the answer to be one ,how can it be possible if no string is created with new keword and nor even with + operator all the strings should lie in the literal pool and the literal poll strings should not get garbage collected at all even if the referance is made null
could somebody pls elaborate ? i am giving exam tomorrow...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sachin,
It is very simple to check which objects are eligible for GC.
Your code with explaination;
String name; //name var is assigned nothing
String newname="nick"; //newname var is assigned "nick"
newname="jason"; //newname var contents has been changed to "jason"
name="fred"; //name var has been assigned "fred"
String newsname=name; //newsname var as well as name var pointing to same memory

name=null; //name is set to null so one object is available for GC
//line a
I always draw picture to do these sort of questions.
I hope this helps.
Anshu
 
sachin agashe
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is ok but what is regarding strings with new operator and without new operator ,
in the problem under consideration there is no object defined with new keaword so no ibject should get gcd even if the reference is srt to null would somebody pls flash on this concept
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sachin,
it seems that you are miss-understanding the concept of an Object.
an object is created even if you don't use the new keyword.
for example, in our code, we have 3 Object variables
namely: name,newname and newsname
and also we have 3 Objects, namely: "nick","jason",and "fred"
these Objects are stored inside the java heap, and are referenced by the Object variables.
so infact when we write the code String name = "myname";
we have already created an Object variable called name, referencing the Object "myname".
the new keyword, comes into play if you want to specifcally tell
the compiler to creat a new and different object.
for example
say we have the code:
String s1 = "hi";
String s2 = "hi";
String s3 = new String("hi");
notic that s1 and s2 refer to the same Object in memory, which is the Obejct "hi", However when we want to creat the same object but in different location in memory we use the new keyword, so s3 actually does not reference the same object referenced by both s1 and s2, it is referencing a new memory location containing the value "hi"
I hope this explain it for you, and i hope I didn't confuse you more with all this memory talking.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SInce there are three objects created namely nick, jason and fred.so garbage collection is with respective to objects not w.r .to object variables so in this case
Only one will be garbage coolected namely the object "nick" since the refernce to this object reference is changed to :"jason" and not bcos name=null is set.bcos when name= null is set the object pointed by name is set to null but still that is reffered by
newsname.so that will not be GCed.
Correct me if i am wrong.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic