my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic
Author

GC

Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362

How many objects are eligible for GC on the line with the comment //here
I don't understand Y the answer is 0
[ April 19, 2004: Message edited by: Mary Cole ]
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
Two objects are being created (besides the BettyAck one): an integer object holding the value 10 and another one holding 99. Vars x and y refer to those objects respectivly.
Doing y = null inside findOut doesn't change the original value of y, so you can pretty much ignore that method all together.
When z comes into play it is made to "point" to the same object y does. So now x refers to the object with 10 and both z and y refer to the other one.
After doing z = null, you still have x and y pointing to the objects. Both objects have a reference to them so they can't be collected.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Mary,
To a method you pass a copy of the value of the reference variable and not the actual object as if you where saying �findOut(y = x )�.
Any manipulation of the referred object is �felt� outside of the method, but any change to the object reference variable like �y=null� is local and has no influence outside of the method.
I�ll diverge a bit, hoping to make it more clear:
Since the Integer wrapper class is immutable, I�ll change the scenario a bit to give an additional example. Suppose your method looked like 'public void findOut(final StringBuffer y)', then you could have been changing the object y in the method findOut, but you wouldn�t have been allowed to change the reference variable with y = null.
hth, Cheers,
Gian Franco
[ April 20, 2004: Message edited by: Gian Franco Casula ]

"Eppur si muove!"
Gianfranco Alongi
Greenhorn

Joined: Apr 20, 2004
Posts: 5
Hey.. just noticed that we almost had the same name..
This is NOT related to java.. but....
uhmmm...
.
.
.
Person me = new Person("Gianfranco Alongi");
Person yu = new Person("Gian Franco Casula");
return me.equals(yu);
.
.
Which we know is false..


Faster machines are a bad excuse<br />for writing unoptimized code<br />Like java...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GC