aspose file tools*
The moose likes Beginning Java and the fly likes Pass by Value (Method arguments) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pass by Value (Method arguments)" Watch "Pass by Value (Method arguments)" New topic
Author

Pass by Value (Method arguments)

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
See also http://jinx.swiki.net/65
Shilpa Bhargava
Ranch Hand

Joined: Sep 15, 2002
Posts: 39
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

Joined: Jul 18, 2002
Posts: 183
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

Joined: Sep 15, 2002
Posts: 39
Got it thanks !!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Count the number of objects created and the scope of the objects and it will be clear.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
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


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Java always passes by value. It's just that for objects the value passed is a reference to an object.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Jul 09, 2001
Posts: 1879

'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

Joined: Jul 09, 2001
Posts: 1180
Thanks a lot!
 
Don't get me started about those stupid light bulbs.
 
subject: Pass by Value (Method arguments)