Here I have a Java doubt regarding the Garbage Collection:
protected class Robocop {
Integer weight = 200;
Robocop attent(Robocop rb) {
rb = null;
return rb;
}
public static void main(String[] args) {
System.out.println("indeed the solution is behind the corner);
Robocop rb1 = new Robocop();
Robocop rb2 = new Robocop();
Robocop rb3 = rb1.attent(rb2);
rb1 = null;
}
}
How many objects do you reckon are going to be eligible for GC?
My take on this, would be 4 object to be garbage collected rb3, rb1 and the related Integer wrapper instance variables.
Inside your method you could as well just return
nullsince you get a copy of reference as argument to your method, not the original reference itself. Thus, you cannot modify original reference inside of your method. You can only modify the object this reference refers to.Right at the end of main, 2 objects will be eligible for GC: one
Robocop(with oneIntegerinside).After main has finished, JVM will just shut down (in your case) and no GC will happen.