Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




[HENRY: Added Code Tags -- and fixed indentation]
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is there to explain?
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since e1 and e2 reference the same object, any operation on e1 is equivalent to an operation on e2.
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21001
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic