This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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
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
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
posted
0
Got it thanks !!
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Count the number of objects created and the scope of the objects and it will be clear.
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
posted
0
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: 1121
posted
0
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
posted
0
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.
'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 ]