jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ///call by reference/// 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 "///call by reference///" Watch "///call by reference///" New topic

///call by reference///

chao-long liao
Ranch Hand

Joined: Jul 29, 2001
Posts: 78
public class Test {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
System.out.println("a is "+ a +"\nb is " + b);
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
I think the output will be a is one more
b is one more,but output is a is one more
b is two.
Does the line " b=a; " do nothing ??
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
note that b is local to swap. Once the method returns, it has no scope

Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Does this mean that the variables declared inside the parameter list of a method is local variable? So I should treat variables in the parameter list same way I would if I declare a variable inside the same method?

Let us be showered in the light of confusion!
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
It depends, primitives will be passed by values and objects will be passed by references. Some object references will allow you to modify the data it is pointing to, like the one here i.e StringBuffer reference a.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Thiru Thangavelu:
It depends, primitives will be passed by values and objects will be passed by references...

Actually, all variables are passed by value in Java. The key to remember is that an object variable doesn't really contain that object, rather, it contains a reference to that object. Therefore, when you pass a object variable to a method, you are really passing a reference to that object. When working with primitives, however, the variable contains the primitive value.
Check out this animation to get a better idea of how parameters are passed in Java.

SCJP Tipline, etc.
Ek Chotechawanwong

Joined: Jun 23, 2002
Posts: 7
You need to understand the differences between object and object reference.
What you pass into swap() method is object reference and it is treated the same way as primitive that is pass-by-value. The reference will not be changed when the method returns.
a and b still refer to its previous object after swap() however the contents of object referred to by a is modified.
Note also that if swap() is defined as

Hope this could help.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Wellcome to the Ranch ekc
Please take a moment to read our name policy and adjust your displayed name accordingly.

SCJP2. Please Indent your code using UBB Code
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Both a and b are objects. And bothe are modified in the methods swap(). SO why arent they behaving the same way. Why has a changed and not b?
Pls clarify.
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
a and b are references to objects. We modified the object referenced by a with the statement a.append(" more"). We never modified the object referenced by b. The statement b=a in swap makes b in swap to refer to the same object as a, but the b in main is still referring to the same object.

Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
From the swap() method:
a.append(" more");
Here, the literal String " more " is appended to StringBuffer a. StringBuffer methods modify the StringBuffer itself, as opposed to String methods, which just return a new String object containing the result of the method. (StringBuffer methods return a reference to the StringBuffer object, which in this case, isn't operated on.)

Here the reference a is assigned to b. Note that the references a & b in the swap() method are not the same as the a & b references in the main() method. At the end of the method, the references in the swap() method go out of scope and are discarded.
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
try this:

use the [CODE] tags - it makes it much easier for people to help you.
Have you checked out Aspose?
subject: ///call by reference///
jQuery in Action, 3rd edition