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: 1824

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.
 
 
subject: Swapping two strings thru method call