# e1=e2 or e2=e1 Logic

Ian Paul Budiman
Greenhorn
Posts: 7
Hi all,

I am a newbie here, and have tried several exercises from Head's First Java, can someone help me with the logic of this:

Where the result is:
helloooo...
helloooo...
helloooo...
helloooo...
helloooo...
46

And I have tried to change the last code to become:
System.out.println(e2.count);

Which resulted the same.

Have examined this for few days but to no avail.

Any help on the logic to get the answer "46" in both cases above mentioned would be much appreciated.

Regards,

[Edit - added code tags - MB]

Joanne Neal
Rancher
Posts: 3742
16
• 1
Ian Paul Budiman wrote:Any help on the logic to get the answer "46" in both cases above mentioned would be much appreciated.

I don't understand. You are getting the answer 46 aren't you ?

Ian Paul Budiman
Greenhorn
Posts: 7
Thanks Matthew for correcting the post and sorry for double post.

I know the answer is 46, but then I tried to solve it manually using logic, but couldn't find one, especially if your println e1 or e2 with the same result.

Maybe I missed something basic since I completely understand the logic for the answer if the initial condition for both e1 and e2 is "Echo e2 = new Echo();" rather then e2=1 or e1=e2.

Thanks fr dropping by.

Matthew Brown
Bartender
Posts: 4568
9
• 2
The main point here is that e1 and e2 are referencing the same object, so e1.count and e2.count are the same variable.

So think of line 21 as e1.count = 2*e1.count;

Now, does the logic make sense? Or is it some other point you're confused over? If so, what output did you expect?

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
• 1
System.out.println() is your best friend when trying to figure out stuff like this. sprinkle them all throughout your code, and print 'interesting' things. I might add one on line 19 to print what e1.count has become after the +1.

Then, I'd put another one before line 21, printing what both e1.count and e2.count are, and ANOTHER one after it, again printing both variables.

You can always comment them out again as things become clearer. I don't delete them entirely, since I often come back and want to see parts again. It's much easier to un-comment them than re-create them.

Eventually, you can put in logging calls, like log4j, which makes it even easier, but that's a little advanced for where you seem to be (no offense).

Ian Paul Budiman
Greenhorn
Posts: 7
It seems the answer is much more simpler than I thought.

I tried to put all the formula into an equation, and got stuck with "e1.count = e1.count + 1"; and "e2.count = e2.count + e1.count;" which then I got lost where to put e1=e2 in the middle.

Thanks for shedding the light.

The answer is: e1.count = e1.count * 2 + 2

And thanks for the input Fred, it's great, it gives me clearer picture......and I'll save log4j later...as your suggestion.

Amey Ambulgekar
Ranch Hand
Posts: 36
• 1
Ian Paul Budiman wrote:Hi all,

I am a newbie here, and have tried several exercises from Head's First Java, can someone help me with the logic of this:

Where the result is:
helloooo...
helloooo...
helloooo...
helloooo...
helloooo...
46

And I have tried to change the last code to become:
System.out.println(e2.count);

Which resulted the same.

Have examined this for few days but to no avail.

Any help on the logic to get the answer "46" in both cases above mentioned would be much appreciated.

Regards,

[Edit - added code tags - MB]

hello Ian Paul Budiman,

Greetings

about your Q that you have posted, there is one thing is that , whenever any object is pointing to any reference variable just like Echo e1 = e2; hence whatever you do operation on either of variable it will be equally reflected to other reference variable or object...

May be you got the point

thanking you..

Winston Gutkowski
Bartender
Posts: 10527
64
• 1
fred rosenberger wrote:System.out.println() is your best friend when trying to figure out stuff like this...

@Ian: and just to add to what Fred said, so is toString(). I generally make it one of the first methods I write for all my classes, eg:and then for testing, wherever you think you need it, you can call:
System.out.println(e1);
which will result in something like:
Echo: count=3

I'll leave you to read up about String.format() (a very useful method).

Winston

Ian Paul Budiman
Greenhorn
Posts: 7
Thanks Winston, I'll look into it, great tips....