jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Can some one explain this to me.  It Display Hello 4x and output is 24 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can some one explain this to me.  It Display Hello 4x and output is 24" Watch "Can some one explain this to me.  It Display Hello 4x and output is 24" New topic
Author

Can some one explain this to me. It Display Hello 4x and output is 24

alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26




[HENRY: Added Code Tags -- and fixed indentation]
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

What is there to explain?


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
I am tying to understand the output is 24. I understand that it will display Hell 4X. But it also Display 24. I am New to Java, i am trying to to figure out how the outcome is 24. if that make any sense.

Hellooooo
Hellooooo
Hellooooo
Hellooooo
24

This is the out put
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Since e1 and e2 reference the same object, any operation on e1 is equivalent to an operation on e2.
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
James Sabre wrote:Since e1 and e2 reference the same object, any operation on e1 is equivalent to an operation on e2.


I still dont understand how e2 becomes 24. im still not grasping it. sorry
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

alex lesta wrote:
James Sabre wrote:Since e1 and e2 reference the same object, any operation on e1 is equivalent to an operation on e2.


I still dont understand how e2 becomes 24. im still not grasping it. sorry


Why do you think it should not be 24?

You have one object and two references to it. You don't have two objects and two references - you have just one object. If you add 1 to e1.count that is exactly the same as adding one to e2.count since both e1 and e2 reference the same object.

If your code had been

then you would have two independent objects, one referenced though e1 and the other through e2.
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
James Sabre wrote:
alex lesta wrote:
James Sabre wrote:Since e1 and e2 reference the same object, any operation on e1 is equivalent to an operation on e2.


I still dont understand how e2 becomes 24. im still not grasping it. sorry


Why do you think it should not be 24?

You have one object and two references to it. You don't have two objects and two references - you have just one object. If you add 1 to e1.count that is exactly the same as adding one to e2.count since both e1 and e2 reference the same object.

If your code had been

then you would have two independent objects, one referenced though e1 and the other through e2.


That i understand but how does e2 has the value of 24. if you have one reference it would come out with a value of 4.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

alex lesta wrote:
That i understand but how does e2 has the value of 24. if you have one reference it would come out with a value of 4.


Actually, no. It won't come out to a value of 4.

I went back and fixed the indentation for you. Maybe that will help.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

since e1` and e2 refer to the same object, e1.count is the same thing as e2.count. so when you get to this line:

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

that is effectively doubling the value that is currently stored. it's the same as writing

x = x + x;


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Try moving the statement:one line up so that it is immediately below the statement
This will place the println() inside the loop and will show you how the e2.count value grows throughout the program. If you're still confused, add more println() statements after every time you change the value.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
You can also try printing both e1.count and e2.count to see that they grow together because e1 and e2 refer to the same object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can some one explain this to me. It Display Hello 4x and output is 24