aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pass-by-reference and pass-by-value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pass-by-reference and pass-by-value" Watch "pass-by-reference and pass-by-value" New topic
Author

pass-by-reference and pass-by-value

Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
What is the difference between pass-by-reference and pass-by-value?


Thanks, AR
SCWCD 5, SCJP 1.4, OCA (PL/SQL)
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
# 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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
You can see this too!


Thanks,


cmbhatt
Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
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

Joined: May 21, 2007
Posts: 17
Thanks! good stuff
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Aug 03, 2002
Posts: 7729
For elite C/C++ programmers: why java does not use "pass by reference".
[ June 10, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
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

Joined: Apr 20, 2006
Posts: 3344

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!


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]
rajesh baba
Greenhorn

Joined: May 19, 2007
Posts: 25
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

Joined: Apr 20, 2006
Posts: 3344

Perhaps, this url may help you to understand the concept clear!

Remember: Java does everything by pass-by-value!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass-by-reference and pass-by-value