I made 3 objects with same reference variable and stored all of them in array, When I print those reference variable all of them show different addresses.I was told if some object loose its reference variable GC removes it from heap, so how come same reference variable showing up 3 difference location, GC did not work? there are actually 3 objects in heap or same reference variable has 3 memory address
class MyClass {
public static void main(String[] args) {
MyClass[] i = new MyClass[3];
for (int j = 0; j <= 2; j++) {
MyClass p = new MyClass();
i[j] = p;
}
for (int k = 0; k <= 2; k++) {
System.out.println(i[k]);
}
}
}
Output :
[shadow@localhost String]$ javac MyClass.java
[shadow@localhost String]$ java MyClass
MyClass@138532dc
MyClass@dce1387
MyClass@54640b25
[shadow@localhost String]$