• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass-by-reference and pass-by-value

 
Aaron Raja
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between pass-by-reference and pass-by-value?
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# pass-by-value
you are using copy of variable (in method)
changes to this copy doesn't affect original variable
in case of reference type: using "." for member access is not considered as change to variable, it is change to referenced object (be careful here).

# pass-by-reference
variable passed by reference doesn't create copy of original variable, it creates alias, which reflects changes to original value
you don't need to worry with pass-by-reference in Java, everything is passed by value
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What John says is exactly the answer you need. But i'll also like to add in that in Java even object references are passed by value.
The behaviour then seems a bit confusing(atleast i was confused a lot in the begining)
but with proper explanation, things will become crystal clear.


Hope this helps
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see this too!


Thanks,
 
Shashank Sharma
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner to scjp...but I did a lot of c and c++ before this .the fundamental of passing variables is same in any object oriented language
So
1)in Pass by Value a copy of orignal parameters are available to the called method
So operations performed on the formal parameters does not reflect back to the orignal values.
Take it like this: This method uses a Photo Stat

2)in Pass by Refrence, the refrence of the orignal parameters are passed to the called method so all the operations done reflects back to the orignal parameters and this uses the orignal documents

well this was all defination stuff
when you pass the actual parameter of primitive data type it is passing by value.
and when you pass the actual parameter as refrence to an object ,it is pass by refrence.I think that will be all
 
Vyas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! good stuff
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shashank,

you should not simplify things too much.

<C++>
Java always uses pointers to objects. If you call a function, a copy of the pointer is passed to it.
</C++>

Even in C and C++, this is passing parameters by value.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For elite C/C++ programmers: why java does not use "pass by reference".
[ June 10, 2007: Message edited by: Barry Gaunt ]
 
Aaron Raja
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what I understand from you guys is
Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.
Java only use Pass by Value!!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra,

The link you have given is for editing and upon being clicked it shows that "only moderators can do this action".

The correct url is here guys!
 
rajesh baba
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
pass by value:
passing a copy of original value.in calling method a new local variable is created and assigned the copy of value.and its scope is with in that method only i.e calling method cannot change original value.
pass by reference:
passing a copy of reference of object or variable that is the calling method creates a new reference to the original object or variable that is any changes made in it that calling method also changes original value
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps, this url may help you to understand the concept clear!

Remember: Java does everything by pass-by-value!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic