-1
class Cardboard{
    Short story = 200;
    Cardboard go(Cardboard cb){
        cb = null;
        return cb;
    }

    public static void main(String args[]){
        Cardboard c1 = new Cardboard();
        Cardboard c2 = new Cardboard();
        Cardboard c3 = c1.go(c2);
        c1 = null;      
    }
}

After go() is executed c2 should be pointing towards null, as implied by the method.This reference of c2 is passed to c3 variable, which again is pointing to null only thus c2's object must be available for garbage collection.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 1
    No, `c2` won't be null. The parameter `cb` will be null inside the method, but that doesn't matter. – Kayaman Nov 02 '17 at 10:20
  • Depending on the JVM / GC implementation, all of c1, c2 and c3 *may be* available to be garbage collected. And maybe even `args` and any strings that it refers to. Questions like this never have a clear-cut answer, and they are generally best not asked in the first place!! – Stephen C Nov 02 '17 at 10:23
  • Why c2 is not null?Reference(copy of c2) is passed in the go() and it makes cb(which is c2) – Krishnan Mishra Nov 02 '17 at 10:23
  • It's not a copy of `c2`s reference. – CraigR8806 Nov 02 '17 at 10:25
  • A whole new object reference gets placed on the stack in a completely different stack frame – CraigR8806 Nov 02 '17 at 10:25
  • 1
    @KrishnanMishra - The assignment `cb = null` does not change the value of `c2`. Java is not call by reference. – Stephen C Nov 02 '17 at 10:25
  • They both point to the same object, but that doesn't mean they are the same reference – CraigR8806 Nov 02 '17 at 10:26
  • @CraigR8806 - The contents of `c2` and `cb` are the same reference (at least to start with). The real explanation is that `c2` and `cb` are not the same variable!! Therefore assigning a new value to one does not affect the other. The references are the values ... – Stephen C Nov 02 '17 at 10:27
  • @StephenC I think we are saying the same thing, but you have a better way of explaining it – CraigR8806 Nov 02 '17 at 10:27
  • 1
    Well yes. But using the correct words is critically important. Easpecially when explaining things to beginners who are liable to get even more confused. That's why I corrected your .... misstatement. – Stephen C Nov 02 '17 at 10:29

1 Answers1

1

That's incorrect. When you pass an object to a method as a parameter, it passes a reference to that object and nothing more. The reference, cb, to the object becomes null, but c2 is still a reference to that same object, so Java still recognizes that the object on the heap has one reference in the code pointing to it: c2.

As Holger pointed out in a comment below, ALL of the objects in this particular snippet are eligible for garbage collection as they are not being used. Java's GC has multiple methods for determining whether an object is eligible to be collected and just the fact that a reference exists that points to the object doesn't shield it from collection, however usage of that object would.

CraigR8806
  • 1,584
  • 13
  • 21
  • "...it passes a reference to that object...": [Java is pass-by-value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Turing85 Nov 02 '17 at 10:31
  • Yes the value is a reference – CraigR8806 Nov 02 '17 at 10:33
  • Java cannot literally pass the object the way that it is structured. It will pass a reference to the object as a value. Not the object itself – CraigR8806 Nov 02 '17 at 10:34
  • It might be worth mentioning that still *all of them* are eligible for garbage collection, as contrary to popular belief, the presence of references is irrelevant to the definition of “garbage”. All that matters, is, whether the object is subsequently used and here, none of the objects is used after the invocation of `go`. Actually, they are unused right from the start, i.e. even before the invocation of `go`, as shuffling around references is not an actual use of the object. Checking for references is only one, simple way to determine that an object can’t be used anymore. But there are others – Holger Nov 02 '17 at 11:22
  • @Holger Thanks! Updated my answer with your info – CraigR8806 Nov 02 '17 at 12:12