• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can someone explain how this code works?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joey Chen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic