This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 1817

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
 
Similar Threads
Interesting problem in Java with pass by reference
Wrapper - Swap
swapping object thru a method
What is diffrence between Singleton and a Nested class
Swapping the string values in java