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...
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
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
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.
A Software life cycle can be greater than its Developer's
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.
"Winners don't do different things<br /> They do things differently"
Destiny's powerful hand has made the bed of my future. And this tiny ad: