• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Reference Variable puzzle

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the solution to this quiz, however I do not understand why the solution is what it is. What are the last seven lines of the code actually doing? To be more specific, I know that hq[x] is referring to the location in the ArrayList, and that the while loop creates objects in positions 1,2, and 3 (which have values 0, 1, and 2), but the solution of what objects are actually being referenced at the end of the code just doesn't make sense to me.

As evident in the code, I did System prints to at least get an idea, but of course that only gets me so far.

Here is the code;


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];
System.out.println("value of hq " + hq[1]);

hq[4] = hq[1];
System.out.println("value of hq " + hq[1]);

hq[3] = null;
System.out.println("value of hq " + hq[3]);

hq[4] = hq[0];
System.out.println("value of hq [4] " + hq[0]);

hq[0] = hq[3];
System.out.println("value of hq [0] " + hq[3]);

hq[3] = hq[2];
System.out.println("value of hq [3] " + hq[2]);

hq[2] = hq[0];
System.out.println("value of hq [2] " + hq[0]);

} //main
} //class
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eva Coppola wrote:I know that hq[x] is referring to the location in the ArrayList



You don't have an ArrayList. You have an array.

Eva Coppola wrote:the while loop creates objects in positions 1,2, and 3 (which have values 0, 1, and 2),



No. Array indexes begin at 0, so you have objects at indexes 0, 1 and 2.

Eva Coppola wrote:the solution of what objects are actually being referenced at the end of the code just doesn't make sense to me.



Why don't you show us what output you got and tell us what you think should have been output and why. That will make it easier for people to see what exactly you don't understand.
 
Eva Coppola
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that Arrays begin at zero, which is what I said -- location 1 in the array is 0, location 2 in the array is 1, etc...

Anyway.

The result for the solution is;

hq[1] is referring to id = 1
hq[3] is referring to id = 2
hq[4] is referring to id = 0

The point here is that in the code, it isn't clear in the assignment statements (hq[3] = hq[1];) whether hq[n] is using the actual literal value of n, or whether it is referring to position "n" in the array. hq[1] could be either position "1" in the array, which would be "0", or it could be the actual value "1", which would be referring to position "2" in the array.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eva Coppola wrote:....................

Here is the code;

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];
} //main
} //class



Lets try to understand with graphical representation.

You have an array of 5. Which means, that array can hold upto 5 objects of HeapQuiz class.
So, when you create an array of 5 using
You get something like this

[ , , , , ]

in memory.

After for loop, what you will have is

[ obj1 , obj2 , obj3 , , ] // Note, the 4th and 5th position is empty. Which means, hq[3] & hq[4] are null]
1.
The hq[3] location which was null will now hold reference of hq[1] which means,
[ obj1 , obj2 , obj3 , obj2 , ]

2.
The hq[4] location which was null will now hold reference of hq[1] which means,
[ obj1 , obj2 , obj3 , obj2 , obj2 ]

3.
The fourth location is made null again. Which means,
[ obj1 , obj2 , obj3 , null , obj2 ]

4.
The last location will now hold value of obj1. Which means,
[ obj1 , obj2 , obj3 , null , obj1 ]

5.
The first location is assigned value of 4th location. Which means, there is no change. Both will still point to same reference.
[ null , obj2 , obj3 , null , obj1 ]

6.
The fourth location to refer to object of location 3
[ null , obj2 , obj3 , obj3 , obj1 ]

7.
Finally, the third location is taking the value of first location.
[ null , obj2 , null , obj3 , obj1 ]

One very important point is, at any statement NONE of the initially created objects (obj1, obj2, & obj3) is going out of scope and they will NOT be garbage collected.
This is the final stage of this operation. Try to print the entire array and you should have similar output.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eva Coppola wrote:I know that Arrays begin at zero, which is what I said -- location 1 in the array is 0, location 2 in the array is 1, etc...



Okay. Guess i misunderstood what you were saying.

Eva Coppola wrote:The point here is that in the code, it isn't clear in the assignment statements (hq[3] = hq[1];) whether hq[n] is using the actual literal value of n, or whether it is referring to position "n" in the array.



hq[0] means the element at position 0 in the array i.e. the first element.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eva Coppola wrote:I know that Arrays begin at zero, which is what I said -- location 1 in the array is 0, location 2 in the array is 1, etc...


I think the confusion comes from not knowing if you mean "index zero" or "the first array entry" when you say "location 1" -- "1" looks too much like an index. For something like this I think it's best to just stick to the use of literal code, hq[0] = 0, hq[1] = 1, and so on. No ambiguity.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Eva Coppola
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your comments - - I do get that the address assignments are changing -

and I know that the array has 5 elements and that 4 and 5 are null. I'm not worried about that. I was having trouble with what objects are being referred to at the end, and what the assignment statements are really doing because the solution provided did not make sense to me when I went through the code myself. Perhaps I am overthinking this...

So, on both sides of the statement, [n] refers to the location in the Array, which is what I thought. Ok.

However - !

This statement in the loop;

hq[x].id = x;

muddied things for me. It is assigning the value of x to the instance variable id of hq[x] .

And as David pointed out, there can be some confusion in whether a reference is being made to an index, or to an actual value.

 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eva Coppola wrote:I know that the array has 5 elements and that 4 and 5 are null. I'm njot worried about that. I was having trouble with what objects are being referred to at the end, and what the assignment statements are really doing because the solution did not make sense.

So, on both sides of the statement, [n] refers to the location in the Array, which is what I thought. Ok.

However - !

This statement in the loop;

hq[x].id = x;

muddied things for me. It is assigning the value of x to the instance variable id of hq[x] .



Oh, I see. I think the sample code was purposefully made confusing for the quiz takers. Just to simplify a bit I would rewrite the problem like this


Which means, each object created will have string variable like
My name is: 0
My name is: 1
My name is: 2

This will avoid possible confusion to resolve this question.
 
I do some of my very best work in water. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic