This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

program statements second  RSS feed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
in the following program:



what does Echo e2=e1; means???


thanks
 
Marshal
Posts: 5593
384
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment.
e2 variable refers to the same object as e1 reference variable.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
ok. that means e1.count=e2.count, e1.x=e2,x, e1.size=e2.size, etc.
is it correct???

thanks
 
Java Cowboy
Sheriff
Posts: 16082
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse, because there is only one Echo object, and both variables e1 and e2 are referring to the same Echo object.

A statement like Echo e2 = e1; does not create a copy of an object. It just makes e2 refer to the same object as what e1 refers to.

Variables of non-primitive types are references to objects, they are not objects themselves. See Cup Size -- a story about variables for a detailed explanation.
 
Liutauras Vilda
Marshal
Posts: 5593
384
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct (if i'd omit incorrect syntax).

e1 and e2 they are both reference variables, which points to the same object. So, actually only one object instantiated in this scenario.

Does it makes sense to you?
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot.
 
Sheriff
Posts: 11745
191
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raj Gurung wrote:Hi,
ok. that means e1.count=e2.count, e1.x=e2,x, e1.size=e2.size, etc.
is it correct???


I just need to clarify because the OP used "=" here instead of "==".

If the OP meant to ask whether the assignment of e2 to e1, Echo e1 = e2, is equivalent to assigning all the fields of e2 to an object referenced by e1, NO, this is not correct. See what Jesper said about there being only one object.

No, Line 1 is NOT equivalent to the rest of the snippet.

If the OP meant to use == instead, then what Jesper and Liutauras said is correct.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!