• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help understanding reference variables linking to reference variables.  RSS feed

 
Ryan Jammes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having difficulty understanding arrays, reference variables and objects. I want to go through some of my understanding, so if anything is wrong please correct me. (Full code from the book below, as well as the (i think) correct output at the bottom of the page)

Q1: My understanding of the code is that the line HeapQuiz[] hq = new HeapQuiz[5]; creates an array of 5 objects of type heapquiz, which is identified as 'hq'. Is this correct?
I went back over it and this apparently only creates an array of 5 references to objects which haven't yet been created? This is kind of like assigning an address to a piece of land where a house(object) hasn't yet been built, right? If this is wrong in anyway please correct me.

Q2: We then have-

-which creates 4 objects of type HeapQuiz? hq[x].id = x; assigns 0 to element 0, 1 to element 1, and so on.

Is hq[x].id=x assigning the value of x to the instance variable 'id' of the object/class? It isn't assigning the value of x to the reference variables in the array right?

Q3: So hq[3] = hq[1] is just a reference variable pointing to the value of another reference variable, correct? Am i correct in understanding though, that in the code below we have both an object hq[1] and an array reference hq[1]?

Q4: I am really having trouble with understanding how reference variables match to their correct ids. I'll start with the ones i think i get first. So according to the code below, [4] = [0], [0] then = [3], [3] then = [2], [2] then = [0]; so [4] ultimately = [0] right? (the id of which is 0) The next element hq[1] is never assigned to another reference variable, so it's instance variable 'id' simply = 1 right?

Now this is where i really start to get lost. How does hq[3] get to = 2 when [2] is = to [0]? Also how does [2] and [0] get to = 1!?!?

Full Code:


Output:

Variable id of hq element 0 = 1
Variable id of hq element 1 = 1
Variable id of hq element 2 = 1
Variable id of hq element 3 = 2
Variable id of hq element 4 = 0
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Jammes wrote:
Q1: My understanding of the code is that the line HeapQuiz[] hq = new HeapQuiz[5]; creates an array of 5 objects of type heapquiz, which is identified as 'hq'. Is this correct?
I went back over it and this apparently only creates an array of 5 references to objects which haven't yet been created? This is kind of like assigning an address to a piece of land where a house(object) hasn't yet been built, right? If this is wrong in anyway please correct me.


Yes, you've allocated space for the references but the references don't as yet refer to anything.

Ryan Jammes wrote:Q2: We then have-

-which creates 4 objects of type HeapQuiz? hq[x].id = x; assigns 0 to element 0, 1 to element 1, and so on.

Is hq[x].id=x assigning the value of x to the instance variable 'id' of the object/class? It isn't assigning the value of x to the reference variables in the array right?


Line 2 creates a new instance of HeapQuiz and then sets reference hq[x] to refer to that instance. Line 3 takes the object instance referred to by hq[x] and sets its field 'id' to x.


Ryan Jammes wrote:Q3: So hq[3] = hq[1] is just a reference variable pointing to the value of another reference variable, correct? Am i correct in understanding though, that in the code below we have both an object hq[1] and an array reference hq[1]?

Not quite. hq[1] refers to an object instance. After hq[3]=hq[1] then hq[3] refers to the SAME object instance. It does NOT mean that hq[3] refers to hq[1]. You could later overwrite hq[1] and hq[3] would still point to the object instance as previously set during hq[3]=hq[1], even though hq[1] now no longer refers to that object instance.


Ryan Jammes wrote:Q4: I am really having trouble with understanding how reference variables match to their correct ids. I'll start with the ones i think i get first. So according to the code below, [4] = [0], [0] then = [3], [3] then = [2], [2] then = [0]; so [4] ultimately = [0] right? (the id of which is 0) The next element hq[1] is never assigned to another reference variable, so it's instance variable 'id' simply = 1 right?

Now this is where i really start to get lost. How does hq[3] get to = 2 when [2] is = to [0]? Also how does [2] and [0] get to = 1!?!?

I'll let someone else walk through this one. Maybe I've given enough info to work this out yourself.
 
Ryan Jammes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to make a correction, the second hq[3] = hq[1]; is actually supposed to be hq[3] = null;

hq[3] = hq[1];
hq[4] = hq[1];
hq[3] = hq[1];
hq[3] = null;
hq[4] = hq[0];
hq[0] = hq[3];
hq[3] = hq[2];
hq[2] = hq[0];

 
Ryan Jammes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Not quite. hq[1] refers to an object instance. After hq[3]=hq[1] then hq[3] refers to the SAME object instance. It does NOT mean that hq[3] refers to hq[1]. You could later overwrite hq[1] and hq[3] would still point to the object instance as previously set during hq[3]=hq[1], even though hq[1] now no longer refers to that object instance.

I'll let someone else walk through this one. Maybe I've given enough info to work this out yourself.


Took me a bit to go over and think about what you said, but i am confident that i now have a decent understanding of the interactions. Thanks for your time!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!