Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between passing by a reference and passing a new object??

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what would be the difference between below statements

Employee e = new Employee();

myMethod(e); // statement 1
myMethod(new Employee) //statement 2

Thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure but one of my friend told me, that whatever values change does by the method is reflect in first but not in second... I tried that but I was getting same values in both case.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which values are you referring to? If you mean `e` then it is just a reference. It will only affect the member variables of the object it references if you alter it in the method.

Otherwise `e` is just a copy of the reference and passed in by value. So only the fields of the object changes, I think.

Taken from the last section here

Though I may be wrong.
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What value changes? How is it related to what you originally asked?
Difference between passing by a reference and passing a new object??
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that I understand, but is there any difference In their execution?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustubh G Sharma wrote:Yeah that I understand, but is there any difference In their execution?

No. The only difference between those two statements is that with the second one there will be no reference to the object in the calling code. As soon as the method returns (assuming no new references to it were created in the method) the object will be eligible for garbage collection, whereas in the first one it won't because 'e' is still referencing it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic