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?
🔒 Locked
Passing by value and reference in Java
Advertisement
Original Post
I have the following program:
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
Jay
Does Java pass by reference or pass by value?
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
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
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
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.
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.
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]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement