One of the hardest things for me to get in my brain was that in
java, you're really not ever dealing with objects directly. all of your named variables really hold
references to objects. So, when I say
Integer i = new Integer(5);
two things get created. the object itself is build off somewhere in what's called the 'heap' (don't worry about that too much - just know you don't have the actual object). I don't have direct access to it.
At the same time, a "reference to an Integer object" is created on what's called the 'stack' (again, don't worry about that too much yet). This reference, which i've called 'i', holds nothing more than the directions on how to get to the real object. It's like an address or a phone number. I think of it as a Rolodex card (if you know what those are) with "i" as the name, and then the memory address instead of a street address.
So...when you say i.getValue(), you're really saying "use the address on the 'i' index card, go there, and use what you find there to run the getValue() method".
=======================
HeapQuiz[] hq = new HeapQuiz[5];
Your HeapQuiz array does the same thing... each element in the array really only holds the address of where some real object is - not the actual object. Think of it as creating a rolodex with five blank
cards in it. The whole rolodex can be found by using the hq reference. 'hq' refers to the actual box/door/wheels/etc. by using the brackets [], you can get to a specific card inside the rolodex.
you then execute this loop:
Inside this loop, you create a new HeapQuiz object (somewhere off in the heap) with the "new HeapQuiz()" statement. You then write the address of that object on the card hq[x]. So, when this loop is done, the first three
cards in your rolodex now all have an address written on them. Each has a different address, since you created three new, distinct objects. The last two cards, indexes 3 and 4, are still blank.
Lets think of the three objects as A, B, and C. Your array looks like this:
[0]->address of A
[1]->address of B
[2]->address of C
[3]->null
[4]->null
Then you get to this line:
All this is saying is "copy the address written on card hq[1] and write it on hq[3]". So at this moment, card 0 points to one object. Card 2 points to a second object. BOTH cards 1 and 3 now point to the SAME actual object, wherever it is.
[0]->address of A
[1]->address of B
[2]->address of C
[3]->address of B
[4]->null
The diagram attempts to illustrate that [1] and [3] POINT TO the same object. They don't CONTAIN the object, but just it's address. There is only one B object.
Now you copy the address from card 1 onto card 4. So now, all three (1,3,4) cards all have the same address on them. You can use any of the three to get to the same object. If you have two cards in your rolodex with my home address, you could use either one to send someone to my house. If the first person broke my front window, the second person would arrive at a house with a broken window.
[0]->address of A
[1]->address of B
[2]->address of C
[3]->address of B
[4]->address of B
(still only one B object, with three things telling you how to get there.
This simply erases what's on the card. card 3 is now blank. You can use it later by writing a new address on it.
[0]->address of A
[1]->address of B
[2]->address of C
[3]->blank or null
[4]->address of B
The rest of the code just does the same thing. It keeps copying new address on top of old ones, which erases the old data.
One thing to remember, though, is when you get to something like this:
you write the address of 0 on 4. you then write the address of 3 on 0. This does NOT change what's already written on 4. So, after each step, you'd have this:
[0]->address of A
[1]->address of B
[2]->address of C
[3]->blank or null
[4]->address of B
hq[4] = hq[0]; gives
[0]->address of A
[1]->address of B
[2]->address of C
[3]->blank or null
[4]->address of A
hq[0] = hq[3]; gives
[0]->null
[1]->address of B
[2]->address of C
[3]->blank or null
[4]->address of A
==============================
Disclaimer...I did this pretty fast, so I may not have all the details right. I may have mixed up an 'A' for a 'B', etc. Just go through it step by step, and trace it out. You'll be fine.