| Author |
Can someone explain how this code works?
|
Joey Chen
Greenhorn
Joined: May 13, 2007
Posts: 15
|
|
I'm trying to figure out how this code results in a number of 24 at the end... Part of the reason I really don't understand how it arrives at 24 is I don't really understand how "Echo e2 = e1;" works. Can someone clarify that? I'm assuming object reference e2 takes on the same values as e1?
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
To your query,
I don't really understand how "Echo e2 = e1;" works. Can someone clarify that? I'm assuming object reference e2 takes on the same values as e1?
Yes. once you say e2=e1, the object referred by the reference variable e1 is assigned to the reference variable e2 as well. After the execution of this statement, both the reference variables e1 and e2 will be pointing to the same object thereby resulting a reflection on the same object irrespective of the reference variable through which you proceed. HtH.
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
your variables e1 and e2 are really both pointers to objects. they're like pieces of paper with your bank account number on them. so when you say "Echo e2 = e1", your saying "give me a new piece of paper with same bank account number written on it as is written on that first one." so now, you have two slips of paper, both referring to the same account. so, any time you use EITHER one to add a dollar to your account, the SAME account gets more money.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Joey Chen
Greenhorn
Joined: May 13, 2007
Posts: 15
|
|
Originally posted by Fred Rosenberger: your variables e1 and e2 are really both pointers to objects. they're like pieces of paper with your bank account number on them. so when you say "Echo e2 = e1", your saying "give me a new piece of paper with same bank account number written on it as is written on that first one." so now, you have two slips of paper, both referring to the same account. so, any time you use EITHER one to add a dollar to your account, the SAME account gets more money.
Thanks, great explanation!
|
 |
 |
|
|
subject: Can someone explain how this code works?
|
|
|