This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Call by ref . & call by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Call by ref . & call by value" Watch "Call by ref . & call by value" New topic

Call by ref . & call by value

dennis foo

Joined: Jul 11, 2005
Posts: 4
If anyone have good links(differentiate them with good examples) for call by value & call by ref.
plz reply me.

I have only few more days for my exam i am totally confused with simple questions like this.
Plz help me
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Java does not pass any parameters by reference , all params are passed by values.
If you are calling a method that takes a reference as a param, a copy of the reference is sent to the method.

[ July 12, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
there is nothing called "call by reference" in java.

Say with me... "Java is not C++"

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass a double to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter's value without affecting values in the code that invoked the method. For example:

The following output illustrates that the value of arg inside halveIt is divided by two without affecting the value of the variable one in main:You should note that when the parameter is an object reference, the object reference -- not the object itself -- is what is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it. Here is an example to show the distinction:

This program produces the following output: Notice that the contents of the object have been modified with a name change, while the variable sirius still refers to the Body object even though the method commonName changed the value of its bodyRef parameter variable to null. This requires some explanation.

The following diagram shows the state of the variables just after main invokes commonName:

At this point, the two variables sirius (in main) and bodyRef (in commonName) both refer to the same underlying object. When commonName changes the field, the name is changed in the underlying object that the two variables share. When commonName changes the value of bodyRef to null, only the value of the bodyRef variable is changed; the value of sirius remains unchanged because the parameter bodyRef is a pass-by-value copy of sirius. Inside the method commonName, all you are changing is the value in the parameter variable bodyRef, just as all you changed in halveIt was the value in the parameter variable arg. If changing bodyRef affected the value of sirius in main, the "after" line would say "null". However, the variable bodyRef in commonName and the variable sirius in main both refer to the same underlying object, so the change made inside commonName is visible through the reference sirius.

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. If the Java programming language actually had pass-by-reference parameters, there would be a way to declare halveIt so that the preceding code would modify the value of one, or so that commonName could change the variable sirius to null. This is not possible. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

Quoted directly from:
Arnold, K., Gosling J., Holmes D. (2000). The Java� Programming Language Third Edition. Boston: Addison-Wesley.

<edit>Interesting - the code wasn't in a monospace font last time I visited. Glad it's back...
[ July 12, 2005: Message edited by: Steve Morrow ]
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Say with me... "Java is not C++"

Bert bates will see this
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Oh yes srini we all read bates and that is what we the readers are expected to say.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Tony Morris
Java Q&A (FAQ, Trivia)
It is sorta covered in the JavaRanch Style Guide.
subject: Call by ref . & call by value
Similar Threads
regarding pass by reference
JComboBox in JTable
Is this enough ?
Difference between call by value and call by ref.