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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

Greenhorn
Posts: 5
• • Number of slices to send:
Optional 'thank-you' note:
• • Dear forum members:

I'm trying to figure out an exercise in a Java-How-to book. Can't figure out why the code below gives "e2.count" total as "7". Can someone show me by adding up the "e1's" and "e2's"? I know it revolves around the "Echo e2 = e1;" line of code, but I can't figure out how it adds to equate to "7." I've tried figuring it out by manipulating the "if (x...)" values to watch the count step-by-step, but when I get to the "x" values in the code below, I lose it.

author and iconoclast Posts: 24203
44   • • Number of slices to send:
Optional 'thank-you' note:
• • Hi,

Welcome to JavaRanch!

The line

Echo e2 = e1;

means that e2 is just another name for e1, and in fact you can delete this line and change all "e2.count" to "e1.count" without changing the program in the least. Therefore the line

e2.count = e2.count + e1.count;

actually means "multiply e1.count by 2"! Does this make more sense now?

David Vimsekh
Greenhorn
Posts: 5
• • Number of slices to send:
Optional 'thank-you' note:
• • No it doesn't. How can e1 * 2 = 7, when numbers are all integers? Watch...

When x = 0

e1 = 1
e2 = 1 + 1 (e2 = 2)

when x = 1

e1 = 2
e2 = 2 + 2 (e2 = 4)

Ok. If I manipulate the variable x to be x < 2 in both cases, then the answer is 6... why? Why isn't it 4? This is why I asked for someone to just show the numbers associated with the "e's" so I can follow along. I would guess that the previous e2 is being added to the e2 in the above case to equate to 6, but that doesn't work in the case below.

when x = 2

e1 = 3
e2 = 3 + 3 (e2 = 6)

Ok. Here the statement e2 = e1 + e2 doesn't apply because it's ran only when x < 1.

Would someone just plug in the e1 and e2 values in each step so I can follow along?

Thank You

Ranch Hand
Posts: 212
• • Number of slices to send:
Optional 'thank-you' note:
• • One key is that e1 and e2 point to the same object.

x=0

e1.count=e2.count = 0 //we will just call e1.count and e2.count, count from here on out

count = count + 1 //(0+1=1)
count = count + count //(1+1=2)
x=1
//end of first loop

count = count + 1//(2+1=3)
count = count + count//(3+3=6)
x=2
//end of second loop
count = count + 1//(6+1=7)
x == 2 so the if block does not execute
x=3
//end of third loop
//while block exits

walking through the code on paper is usually helpful. Adding in print statements to print out the value at certain points is also helpful.

David Vimsekh
Greenhorn
Posts: 5
• • Number of slices to send:
Optional 'thank-you' note:
• • Yah, I did sit with a piece of paper and work out the steps, but something does not still make sense to me... (but thanks so far)...

It says: Echo e2 = e1

I read that as: "e2 becomes what e1 is"
In other words: e1 value --> e2

but not as: "e2 becomes what e1 is and e1 becomes what e2 is"
Same as: e1 value --> e2, e2 value --> e1

It's harder to discusss it with your example because you lumped together e1 and e2. But going back to the way it should look...

So when you say: e2.count + e1.count = 6 (which means e2 = 6)
then you say: e1.count + 1 = 7 (you just plugged in e2's value in for e1)

Which means what value is plugged in for e1 becomes e2, and vice-versa then?

How would one state that "Echo e2 = e1" but "e1 never becomes what value e2 holds" then?

meaning: e1 value --> e2, legal
but: e2 value --> e1, illegal

Is it possible to do that?

I should mention where I'm confused:

if "x = 0" in code, it doesn't mean that I can code "0 = x" and manipulate it as such in a Java program.
[ February 27, 2008: Message edited by: David Vimsekh ]
[ February 27, 2008: Message edited by: David Vimsekh ]

Marshal Posts: 74712
335
• • Number of slices to send:
Optional 'thank-you' note:
• • Echo e1.count? You mean Echo e1; otherwise it would think e1.count is an Echo, rather than e1.

You start off with e1, which you have already been told has a count of 0.
Then you set up e2 and tell it to use the same object that e1 is already using. That is what e2 = e1; means. Nothing about vice versa; the assignment operator = works in one direction only, right-to-left, never left-to-right. As you said, you can say x = 0; but you can't say 0 = x;.

No, e1 never becomes what e2 used to be (e2 never had a value "of its own"). Try again; David McCombs has already explained how the loops work, and see if you can get 7 out of it. Rancher
Posts: 3742
16
• • Number of slices to send:
Optional 'thank-you' note:
• • You seem to be thinking that e1 and e2 are objects - they are not - they are object references.
creates an Echo object and sets e1 to reference this object.
sets e2 to reference the same object as e1. There is still only one Echo object which you can access using either e1 or e2.

Does that help ?

David Vimsekh
Greenhorn
Posts: 5
• • Number of slices to send:
Optional 'thank-you' note:
• • Ok hold on here....

If it's not vice versa, then I'm totally lost. I have read what McCombs wrote...
But this is how I read it...

[ February 28, 2008: Message edited by: David Vimsekh ]
[ February 28, 2008: Message edited by: David Vimsekh ]

Greenhorn
Posts: 5
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by David McCombs:
One key is that e1 and e2 point to the same object.

x=0

e1.count=e2.count = 0 //we will just call e1.count and e2.count, count from here on out

count = count + 1 //(0+1=1)
count = count + count //(1+1=2)
x=1
//end of first loop

count = count + 1//(2+1=3)
count = count + count//(3+3=6)
x=2
//end of second loop
count = count + 1//(6+1=7)
x == 2 so the if block does not execute
x=3
//end of third loop
//while block exits

walking through the code on paper is usually helpful. Adding in print statements to print out the value at certain points is also helpful.

Thanks for the tip about add print statements to print out the values. Is really helpful and make the understanding more easy.

Ernest Friedman-Hill
author and iconoclast Posts: 24203
44   • • Number of slices to send:
Optional 'thank-you' note:
• • As I said way back in the first reply on this thread:

The line
Echo e2 = e1;
means that e2 is just another name for e1,

I guess if you don't understand about reference variables, this isn't specific enough. I also said that you should replace all "e2" with "e1" after this, since they're equivalent, and you said "that's too confusing". But in fact, that's the central point of this example. Try reading this and this. These two amusing short essays should hopefully clear this all up for you!

Joanne Neal
Rancher
Posts: 3742
16
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by David Vimsekh:

e1 and e2 both point to the same object (see my previous post), therefore as soon as you set e2.count to a value, e1.count will have the same value (because it is the same variable).

David Vimsekh
Greenhorn
Posts: 5
• • Number of slices to send:
Optional 'thank-you' note:
• • I guess if you don't understand about reference variables, this isn't specific enough. I also said that you should replace all "e2" with "e1" after this, since they're equivalent, and you said "that's too confusing". But in fact, that's the central point of this example. Try reading this and this. These two amusing short essays should hopefully clear this all up for you!

Thank you. The second essay cleared it up. Head Start should realize the "beginner" programmer won't understand this off the top of their head. I know it's a bonus question, but how am I supposed to figure that out by myself when the book up to this point hadn't even alluded to how to understand this?

Thank You to everyone who helped me nail this one down. I can now move on to the next chapter. I'm looking forward to helping anyone in this forum when I can do so.

Campbell Ritchie
Marshal Posts: 74712
335
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by David Vimsekh:
Thank You to everyone who helped me . . .

You're welcome, and I am sure that applies to everybody. It is good to see we have been sorted things out successfully.  WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton