Someone teach me line by line how to read this particular code

Glenn Delostrico
Greenhorn
Posts: 7
Class HeapQuiz {
int id = 0;
public static void main(String[] args) {
int x - 0;
HeapQuiz [] hq = new HeapQuiz[5];
while ( x < 3) {
hq[x] = new HeapQuiz();
hq[x].id = x;
x = x + 1;
}
hq[3] = hq[1];
hq[4] = hq[1];
hq[3] = null;
hq[4] = hq[0];
hq[0] = hq[3];
hq[3] = hq[2];
hq[2] = hq[0];
//do stuff
}
}

Ok this is what I understand so far.
while x < 3 the program will run.
hq[x] = new HeapQuiz (); this means it will take on the value of HeapQuiz
hq[x].id = x; this means id = the value of x
x = x + 1; in the beggening x = 0 in this case x is now 1...
This is where I stop understanding and start guessing

My guess:
since in the beggining x = 0 does that mean hq[4] = h[0] and hq[2] = hq[0]; ? if so as long
as the program runs I'm assuming when x = 1 hq[3] = [hq1] and hq[4] = hq[1]?
when x = 2, the answer is hq[3] = hq[2]

I dont want to compile the program I just want to learn how to read java, compiling is sort of cheating when your trying to learn...

Glenn Delostrico
Greenhorn
Posts: 7
I'm sorry here is the full code below I copied it wrong.

public class HeapQuiz {
int id = 0;
public static void main(String[] args) {
int x = 0;
HeapQuiz [] hq = new HeapQuiz[5];
while ( x < 3 ) {
hq[x] = new HeapQuiz();
hq[x].id = x;
x = x + 1;
}
hq[3] = hq[1];
hq[4] = hq[1];
hq[3] = null;
hq[4] = hq[0];
hq[0] = hq[3];
hq[3] = hq[2];
hq[2] = hq[0];
//do stuff
}
}

Greg Brannon
Bartender
Posts: 563
Why are you starting with this code as a starting point for your Java experience? The code you chosen involves topics that are often not covered until Chapter 7 or beyond of most Java books that I've surveyed.

Some of what you think you understand isn't accurate. There's an order to understanding programming, and you've skipped a few steps. If you're open to the idea, I recommend you find a new starting point, a basic Java tutorial like this one.

Glenn Delostrico
Greenhorn
Posts: 7
>.<

This is chapter 4 of head first Java:

Hello world application below:

public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World!");
}
}

The book doesn't really break it down if you guys know a truly begginers guide to java please let me know.

Greg Brannon
Bartender
Posts: 563
I haven't used that specific book, but it's highly recommended here as a beginner's text. I'm using another Head First book and find it goes to great lengths to explain the details. If you're not getting that from your Head First Java experience, I'm not sure how to do it better than the book, though I realize that sometimes getting another slant on the same topic can be helpful. If there's something you don't understand about the HelloWorld AP, please be specific.

Cody Hey
Greenhorn
Posts: 4
EDIT: I didn't see the bottom of your first post, I decided I'm going to just copy the code and label it with comments (//hopefully you know, this is a comment)

OLD --------------

If your still curious:
Right now it doesn't really do anything, for all the things with brackets like "hq[]" mean it is an array(a list of numbers, in this case) and this program defines the first few slots in the list (hq[1],hq[2] ect.)

Other than that the line "public class..." is the basic thing that names the file
"public static void main" is the first method of the "class" ( a method is just a chunk of code.

I'm no java pro but I do know pretty intermediate stuff
Any questions, just ask, I'll clarify.

Glenn Delostrico
Greenhorn
Posts: 7
Well I understand the Hello world...its basic in any programming language...when the compiler runs its telling you to write Hello World.

And I understand the code snippet I posted doesnt really do anything. I just want to be able to think like a compiler. I want to know what the compiler is thinking. In my book I'm suppose to match the id=0, id=1, and id=2 to the corresponding hq[x]. I thought I understood the program but when i checked the answer its wrong. I want to know why I'm wrong.

Since id=0 i thought that means basically when x=0 and when x = 0 it corresponds to the right of the hq[x] bracket. So my logic goes when x = 0 it is the same as id = 0 which is the same as hq[4] = hq[0] and hq[2] = hq[0]...etc

I dont want to go to the next chapter until I know for sure I understand the logic in this particular code.

Glenn Delostrico
Greenhorn
Posts: 7
Hey cody your post was very helpful it helped alot when you added your comments. So when you have to match id=0, id=1, and id=2 how do you come up with the answer?

Cody Hey
Greenhorn
Posts: 4
Hq[2]= null
Hq[4]=0
hq[0]=null
Hq[3]=2

If I'm right I'll explain how I did it
(remember to keep in mind changes, like hq3 is changed to null THEN hq0 is set to hq3 aka null)

Matt Hanrahan
Greenhorn
Posts: 14
Hi Glenn,

Look back at the dog array a few pages back. The main point of this puzzle is to see how reference variables can change what object they point to.

It helped me to draw it out in pencil and to make and erase lines as I went down the lines.

This makes the reference variables

This assigns the first three only. On your picture draw a line like this:
hq[0] -> id=0
hq[1] -> id=1
hq[2] -> id=2

The rest of the code reassigns the reference variables.
hq[3] = hq[1];
draw a line from hq[3] -> id=1 , because hq[1] points to id=1

hq[4] = hq[1];
draw a line from hq[4] -> id=1 , same logic as previous, because hq[1] points to id=1

hq[3] = null;
erase line from hq[3] -> id=1, hq[3] has no line to an object now

hq[4] = hq[0];
erase line from hq[4] -> id=1 and draw new line from hq[4] -> id=0 , a reference variable can only point to one object

hq[0] = hq[3];
erase line from hq[0] -> id=0 but don't draw new line. hq[3] is null so it has no line, making hq[0] = hq[3] means hq[0] is null now

hq[3] = hq[2];
draw line from hq[3] -> id=2 , because hq[2] points to id=2

hq[2] = hq[0];
erase line from hq[2] -> id=2, don't draw a new line because hq[2] is null so it has no line, making hq[2] = hq[0] means hq[2] is null now

On this problem, a tip states "Unless you're way smarter than we are, you probably need to draw diagrams like the ones on page 57-60 of this chapter." etc.

I am over 400 pages into this book and can say I have learned a lot. The important thing to keep in mind are the details.
Computer programing like math is a "dense" language, every detail is important, so I've found it helpful to make sure I don't miss any step.

The beginning chapters were harder for me, but I only moved on after I was pretty satisfied that I had a good understanding of the fundamentals there.
It is actually getting easier now (I'm finishing CH. 13 now).

I hope the explanation helps somewhat, if I can help further let me know.

-Matt

Glenn Delostrico
Greenhorn
Posts: 7
Matt,

Your the best, I totally understand it now. At first I was confused because you are left id=1 not having a line to somewhere.

I forgot your above statement hq[1] ->id=1 and the rest just reassigns the value. So id = 1 kept the same value that it had before because the reassign values nulled them selfs out.

awesome...you the best!!!

Matt Hanrahan
Greenhorn
Posts: 14
Glad to help. If you have a post that you want me to look at PM me as I don't watch all new posts but would be glad to help if I can.

-Matt

Winston Gutkowski
Bartender
Posts: 10527
64
Slightly off-topic, and at the risk of telling you something you already know, I'd also suggest remembering that indexes in Java always always always start from 0; so, for any array a of length n, its elements go from a[0] to a[n-1].

This is also true of Lists, numeric digits, memory addresses, HashMap buckets; and even crops up in the most annoying places like Calendar.MONTH (January is month 0).
It's also why for loop syntax is the way it is. Eg, to go through the above array, you use:
for (int i = 0; i < a.length (NOTE: less than); i++) {...

It was one of the hardest things to remember when I was starting out, because we puny humans tend to number things from 1; but once you get used to it, you'll find it's actually quite nice.

Winston

Matt Hanrahan
Greenhorn
Posts: 14
Winston,

Very true, I was burnt a few times at first.

Thanks for pointing it out, this is the perfect place to add any helpful notes.

Thanks,
Matt