Eligible objects for garbage collection


This question already has an answer here:

  • Objects eligible for garbage collection 5 answers
interface Animal { void makeNoise(); } 

class Horse implements Animal {
  Long weight = 1200L;
  public void makeNoise() { System.out.println("whinny"); }

public class Icelandic extends Horse {

  public void makeNoise() { System.out.println("vinny"); }
  public static void main(String[] args) {
    Icelandic i1 = new Icelandic();
    Icelandic i2 = new Icelandic();
    Icelandic i3 = new Icelandic();
    i3 = i1;
    i1 = i2;
    i2 = null;
    i3 = i1;
    //<-- HERE

When HERE is reached, how many objects are eligible for the garbage collector? And why?

Four objects can be cleaned up. There are two Icelandic objects which are no longer referenced and two Long objects. One Icelandic and one Long and the String[] can still be referenced.

Note: future versions of Java could auto-box Longs like 1200L in which case the answer would be just 2 as the Longs would still be referenced. e.g. you changed the example to Integer the answer would depend on command line arguments.

BTW: I didn't know Icelandic was a type of Horse. Perhaps IcelandicHorse would be less confusing.