This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Swapping the string values in java  RSS feed

 
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
 
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.
 
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.
 
(instanceof Sidekick)
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?
 
Java Cowboy
Posts: 16084
88
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!