Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

///call by reference///

 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
note that b is local to swap. Once the method returns, it has no scope
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Corey
 
Ek Chotechawanwong
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Ek
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wellcome to the Ranch ekc
Please take a moment to read our name policy and adjust your displayed name accordingly.
thanks
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thx
 
Tybon Wu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

b=a;
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic