aspose file tools*
The moose likes Java in General and the fly likes Swapping two strings thru method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Swapping two strings thru method call" Watch "Swapping two strings thru method call" New topic
Author

Swapping two strings thru method call

Ashish Kothari
Ranch Hand

Joined: May 16, 2003
Posts: 37
I want to swap two strings thru a method like swap(String str,String str1).
but passing thru reference does not work,do you know How can I swap values of String thru method.
Is this possible as strings are immutable ?
Ashish
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
hi:
please refer to:
Class SWAP;
swap;
Does Java pass by reference or pass by value?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Originally posted by Ashish Kothari:
I want to swap two strings thru a method like swap(String str,String str1).
but passing thru reference does not work,do you know How can I swap values of String thru method.
Is this possible as strings are immutable ?
Ashish

Java passes by value, not be reference, so swap(String str, String str1) will not work. You could write a method like this:

Note that you've simulated "pass-by-reference." This method is a bit obtuse in this example, but in other cases it can work wonders.
[ June 10, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swapping two strings thru method call
 
Similar Threads
What is diffrence between Singleton and a Nested class
Interesting problem in Java with pass by reference
swapping object thru a method
Swapping the string values in java
Wrapper - Swap