File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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
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

Joined: Jun 06, 2007
Posts: 2851

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

|Asking Good Questions|
Chandra Bhatt
Ranch Hand

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


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
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

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.

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

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: 3381


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

Joined: May 19, 2007
Posts: 25
pass by value:
passing a copy of original 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: 3381

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:
subject: pass-by-reference and pass-by-value
It's not a secret anymore!