Skip to main content
gamedev.net
🔒 Locked

Passing by value and reference in Java

Started by yaroslavd Apr 18, 2004 at 1:33 PM 15 replies 2.2k views
Original Post
yaroslavd
yaroslavd
I have the following program:
public class test {
	public static void main(String[] args) 
	{
	 String s= "world ";
	 int n = 6;
		
		changer(s,n);
		
		System.out.println(s+" - "+n);
	}
		
	public static void changer(String x, int y) {
		x = x + "peace";
		y = y * 2;
	}
}
I always thought Strings, as subclasses of Object, were passed by reference, and ints, as value types, were passed by value. However, this program prints out worldpeace - 6, which would mean that Strings are passed by value. Help?
Jason Zelos
Jason Zelos
Other way around, If strings were passed by value the peace bit would be lost when the function ended, as it would be a local copy of the var.

Jay
yaroslavd
yaroslavd
Yes, I''m sorry, that''s what I meant. It prints out world - 6.
yaroslavd
yaroslavd
Oh, I see. I''m assigning a new value to s. Thus, I''m switching its value from the parameter reference to a new value. Got it. Thanks.
markr
markr
Strings are immutable so you can''t modify them even though they''re passed by reference.

However, the = operator assigns the reference not the value in the case of objects, so using it creates a brand new string and does not modify the old one (which it cannot, as they are immutable).

Mark
yaroslavd
yaroslavd
Right. Just to clarify, this would happen with any Object, not just String, right?
yaroslavd
yaroslavd
Also, can someone write a swap(Object x,Object y) function for me? How would you do this if everything is passed by value?
Miserable
Miserable
quote:
Original post by yaroslavd
Also, can someone write a swap(Object x,Object y) function for me? How would you do this if everything is passed by value?

You can''t. Since everything is passed by value, there is no general way. The only way of doing this is to define semantics to swap the internals of the two Objects.
HenryApe
HenryApe
quote:
Also, can someone write a swap(Object x,Object y) function for me?

You can''t write a function that will do:
Object x = new SomeObject();
Object y = new SomeOtherObject();
swap(x,y); // meant to assign x to y and vice versa

The solution that the AP posted will just swap the local variables inside the swap method. It will have no effect on the references in the method that calls the swap method.
yaroslavd
yaroslavd
Thanks for the answers, guys. They certainly clarified my misunderstanding of this Java concept. My "Beginning Java" book gives me some doubts now.
HenryApe
HenryApe
quote:
The only way of doing this is to define semantics to swap the internals of the two Objects.

Actually, the most common way is probably just to give the swap method access to the object that holds the two objects to be swapped, either by defining the swap method in the object container class or by explicitly passing the container along.

The most common use of swapping is probably to make two elements in a data structure change position and you would normally implement that by allowing the swap method to access the data structure (not by copying the internal data of the elemtents back and forth).
quote:
the only way would be
void swap( Object[] f ) {

Using an array as a container object for the swaps is a special case of the solution above, it's not the only way (you can use a list, a tree, or any arbitrary object that gives the swap method access to its data).

[edited by - HenryAPe on April 18, 2004 3:43:00 PM]
Zahlman
Zahlman
Just a final note, you can still call mutators on non-primitive parameters. So you would acheive world peace (heh) if you used a StringBuffer instead of a String, and the "append()" method to change the buffer''s contents.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.