Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding Objects and Reference Variables

 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is my first post here. I'm trying to understand objects. On page 66 in Head First Java is a problem called "Heap o' Trouble". I'd like some confirmation that my understanding is correct. Following, is the code with a couple things I added in playing around with it:

class HeapQuiz{
int id = 0;
public static void main(String[] args){
int x = 0;
HeapQuiz[] hq = new HeapQuiz[5];

for(int i = 0; i < 5; i++){
System.out.print(hq[i] + " ");
}

while(x < 3){
hq[x] = new HeapQuiz();
hq[x].id = x;
System.out.print(hq[x].id + " ");
x++;
}
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];

for(int i = 0; i < 5; i++){
System.out.print(hq[i] + " ");
}
}
}

OK, as I understand it, first an array called hq is created. The type of array is a HeapQuiz type (as opposed to a primitive type). The array variable hq would be a reference variable. There's nothing in the array at this point and there are no HeapQuiz objects that the array references. This is demonstrated by the first printing of the array which prints out five nulls. Correct?

Next, the while statement creates three new HeapQuiz objects and assigns them to the first three hq reference variables hq[0], hq[1], hq[2]. It also creates three object id's for each of the objects, so we have HeapQuiz.id 1, HeapQuiz.id 2, and HeapQuiz id 3. The id is really all that's in each HeapQuiz object. Correct?

Then, there's a series of assignment statements that move around the assigments of the objects to different reference variables in the array. Correct?

I added one more print statement just to see what would happen when the array prints out. It prints null for hq[0] and hq[2] because they no longer refer to anything. For hq[1], hq[3], and hq[4] it prints some strange stuff like: HeapQuiz@150bd4d. What is that? Does that represent the reference variable in the array? Or, is it trying to print out the HeapQuiz object itself?

Thank you,
Dave
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave D, welcome to JavaRanch!

First, please check your private messages by clicking on My Private Messages. Thanks!

You are correct in your understanding of this code!

One small point: When you say "three object id's," maybe you just mean id's for the objects. But just to be clear, "id" references a primitive int value and not an object.

Regarding the output of "HeapQuiz@150bd4d"... When you provide an object reference to a println statement, it implicitly calls the toString() method on the object. In this example, HeapQuiz inherits its implementation of toString() from the parent class, Object, which uses...

getClass().getName() + "@" + Integer.toHexString(hashCode())

(Note: hashCode is likely derived from a memory address for the object, so you will probably see different results each time you run this. Don't worry if this part doesn't make much sense. These details will come.)
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Marc, for your reply. Much appreciated!

Regarding your point about the id's, I understand that the id's are not the objects themselves, but are they are connected to, or belong to the objects? Like, does id 0 belong to the object referred to by hq[0]? I'm trying to understand the statement hq[x].id = x;

Also, I was wondering if that statement creates the int variable id, such that we could have named it anything we wanted, like "index"? Also, how does it know to make it an int variable?

Thanks,
Dave
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, each HeapQuiz instance (object) has its own int variable called "id." This is defined inside the class HeapQuiz...

This basically says, "A HeapQuiz HAS-AN int called 'id' (which is assigned an initial value of zero)." The type (int) is immediately before the variable name (id). If you wanted to call this variable something else and/or change the type, this is where you would do it.

As you know, hq[x] references a HeapQuiz object. So hq[x].id represents that object's id. Note that the expression hq[x].id is not defining anything. It is only referencing something that's already been defined.

For example, in the following part of your code, the first line is assigning (writing) a value (x) to hq[x].id, and the second line is reading that value to be displayed.

[ February 09, 2008: Message edited by: marc weber ]
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! Thank you, Marc. That absolutely clears it up for me. I had forgotten all about that id declaration up at the top. This helps me understand objects tremendously! I really appreciate you taking the time to explain this to me.

JavaRanch is a great resource.

Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic