File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Swapping the string values in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Swapping the string values in java" Watch "Swapping the string values in java" New topic

Swapping the string values in java

samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
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;


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
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Return an array/collection which returns the swapped order and use the return value.
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

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.

apigee, a better way to API!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15046

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Swapping the string values in java
It's not a secret anymore!