• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swapping the string values in java

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Return an array/collection which returns the swapped order and use the return value.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic