Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swapping the string values in java

 
samir ware
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
I am not getting the solution for an issue. I want to swap the values of the two string variables. For that I have written the following code.

public static void swap(String a,String b)
{
String temp;
temp = a;
a=b;
b=temp;

}


which I am going to call from my main method .However this will not work as by default java does pass by value.
can anybody tell me the solution so as to swap the values of the two string variables. I dont want to write any code related to swapping in main . Everything related to swaping should be there in swap function

Thanks in advance
samir
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Return an array/collection which returns the swapped order and use the return value.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However this will not work as by default java does pass by value.

Java does a pass by value for primitive data types and by reference for objects. So, inside a method if you make any alterations to the argument that is an object, this will be reflected after the method call. However, since string is an immutable object, so you can not change it in any way.
Answering your question, you can not swap the values of two string references using a separate method.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We prefer to say Java does pass by value - passing a copy of the variable - for everything. When a variable is an object reference it passes a copy of the reference. Once you accept that, "always pass by value" is easy to work with.

Let's call your method like this:

The caller passes str1 to swap(). The JVM makes a copy of the variable: a new reference to "hello" now known to swap() as a. In your swap method, you make a point to "world". That does NOT change the caller's original variable, which still points to "hello".

It's a bit to get your head around, no? Does that help?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitesh Kant: "Java does a pass by value for primitive data types and by reference for objects."

No. This has been discussed before... Java only has pass by value. There is no pass by reference in Java. Note that non-primitive variables in Java are references. If you pass such a value to a method, you pass a reference by value.

So, primitive data types and references are both passed by value.

Note that Sun's Java tutorial has a mistake in it, it says the same thing Nitesh is saying, which is wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic