• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by Value (Method arguments)

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by Value / Pass by Reference?
When I pass an primitive data as parameter of a method and change it in the
method without an object reference it will only change the value of the variable
locally in the method and will not effect the object’s instance variable.
(Pass by value for primitive method arguments).
When I pass an object I can change the references within a method like swap.
StringBuffer is an object so why is there pass by value that is reference b is
only changed to „One more“ in swap() and if swap() is left it is „is Two“ again.
Why?

C:\Java\EigeneJavaProgramme>java Test35
b is One more in swap()
a is One more
b is Two
Thanks for your answers.
Thomas
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because references are passed by value, too. If you change an object a reference is pointing to, that change will be visible to outside the method. If you change the local reference itself, the change won't be propagated.
Does that help?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also http://jinx.swiki.net/65
 
Shilpa Bhargava
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I changed the above code to
public class Test35 {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test35.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
b.append(" less");
System.out.println("\nb is " + b + " in swap()");
}
}
and i get the o/p as
b is One more less in swap()
a is One more less
b is Two
All confused ! ..can someone explain!
Thanks
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it like this:
StringBuffer a = new StringBuffer("foo");
Looks like:

Now when you pass a into a method, you make a copy of a, like:
modify(a); # pass copy of a reference
void modify(StringBuffer b) { ... }

So something like b.append("bar") will modify the StringBuffer on the heap, and the modification will be visibile outside the method.
However, if you change the reference value that b points to, then any changes are no longer visible to the outside world:
b = otherStringBuffer; # breaks link
Hope this clarifies things?
[ September 17, 2002: Message edited by: Neil Laurance ]
 
Shilpa Bhargava
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it thanks !!
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Count the number of objects created and the scope of the objects and it will be clear.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the areas in which Java is much simpler than its predecessors.
In Java, variables can either contain primitive values or refer to objects.
Reference variables will be passed to methods by reference and primitive values will be passed by value. So, the important thing in Java is the fact that this behavior is _inherent_ to the variable.
Cheers,
Dan
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java always passes by value. It's just that for objects the value passed is a reference to an object.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just that for objects the value passed is a reference to an object

is _just_ identical to
Reference variables will be passed to methods by reference

Cheers,
Dan
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a subtle, yet very important difference. If Java passed references by reference then a method that did this:

Would actually be doing something. A copy of the pointer is made and passed to the method. In other words, Java passes the value of the pointer, not the pointer itself.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'passing a reference by value' is significantly different than 'passing by reference'. See some of the commom myths and an easy to understand explanation and comparison between the two here
Jamie
[ September 18, 2002: Message edited by: Jamie Robertson ]
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic