aspose file tools*
The moose likes Beginning Java and the fly likes Can someone explain how this code works? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone explain how this code works?" Watch "Can someone explain how this code works?" New topic
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: 3355

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: 11499
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone explain how this code works?