📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Swapping two strings is a common task in programming. While swapping primitive types like integers is straightforward, swapping objects like strings requires a bit more attention, especially considering that strings in Java are immutable. This blog post will explore different methods of swapping two strings in Java.
Table of Contents
- Swapping Using a Temporary Variable
- Swapping Without Using a Temporary Variable
- Swapping Strings Using a Wrapper Class
- Complete Example Program
- Conclusion
1. Swapping Using a Temporary Variable
The most straightforward method to swap two strings is by using a temporary variable.
Example:
public class SwapStringsUsingTemp { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; System.out.println("Before swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); // Swapping using a temporary variable String temp = str1; str1 = str2; str2 = temp; System.out.println("After swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); } }
Output:
Before swap: str1: Hello str2: World After swap: str1: World str2: Hello
Explanation:
- A temporary variable
temp
is used to hold the value ofstr1
. - The value of
str2
is assigned tostr1
. - The value of
temp
(original value ofstr1
) is assigned tostr2
.
2. Swapping Without Using a Temporary Variable
Swapping strings without using a temporary variable is also possible by concatenating and splitting the strings.
Example:
public class SwapStringsWithoutTemp { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; System.out.println("Before swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); // Swapping without using a temporary variable str1 = str1 + str2; // str1 becomes "HelloWorld" str2 = str1.substring(0, str1.length() - str2.length()); // str2 becomes "Hello" str1 = str1.substring(str2.length()); // str1 becomes "World" System.out.println("After swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); } }
Output:
Before swap: str1: Hello str2: World After swap: str1: World str2: Hello
Explanation:
- The strings are concatenated and stored in
str1
. str2
is then assigned the substring ofstr1
from the start to the length of the originalstr1
.str1
is assigned the substring from the end ofstr2
's length to the end of the concatenated string.
3. Swapping Strings Using a Wrapper Class
When you need to swap strings in a method, a wrapper class can be used to simulate pass-by-reference behavior in Java.
Example:
public class SwapStringsUsingWrapper { public static void main(String[] args) { StringWrapper str1 = new StringWrapper("Hello"); StringWrapper str2 = new StringWrapper("World"); System.out.println("Before swap:"); System.out.println("str1: " + str1.value); System.out.println("str2: " + str2.value); // Swapping using a method and wrapper class swapStrings(str1, str2); System.out.println("After swap:"); System.out.println("str1: " + str1.value); System.out.println("str2: " + str2.value); } public static void swapStrings(StringWrapper str1, StringWrapper str2) { String temp = str1.value; str1.value = str2.value; str2.value = temp; } } class StringWrapper { String value; StringWrapper(String value) { this.value = value; } }
Output:
Before swap: str1: Hello str2: World After swap: str1: World str2: Hello
Explanation:
- A
StringWrapper
class is created to hold the string value. - The
swapStrings
method swaps the values of theStringWrapper
objects. - This simulates pass-by-reference behavior, allowing the strings to be swapped within a method.
4. Complete Example Program
Here is a complete program that demonstrates all the methods discussed above to swap two strings.
Example Code:
public class SwapStringsExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; // Using a Temporary Variable System.out.println("Using Temporary Variable:"); swapUsingTemp(str1, str2); // Using Concatenation and Substring System.out.println("\nUsing Concatenation and Substring:"); swapWithoutTemp(str1, str2); // Using Wrapper Class System.out.println("\nUsing Wrapper Class:"); StringWrapper sw1 = new StringWrapper("Hello"); StringWrapper sw2 = new StringWrapper("World"); swapUsingWrapper(sw1, sw2); System.out.println("After swap:"); System.out.println("str1: " + sw1.value); System.out.println("str2: " + sw2.value); } public static void swapUsingTemp(String str1, String str2) { System.out.println("Before swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); String temp = str1; str1 = str2; str2 = temp; System.out.println("After swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); } public static void swapWithoutTemp(String str1, String str2) { System.out.println("Before swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); str1 = str1 + str2; str2 = str1.substring(0, str1.length() - str2.length()); str1 = str1.substring(str2.length()); System.out.println("After swap:"); System.out.println("str1: " + str1); System.out.println("str2: " + str2); } public static void swapUsingWrapper(StringWrapper str1, StringWrapper str2) { System.out.println("Before swap:"); System.out.println("str1: " + str1.value); System.out.println("str2: " + str2.value); String temp = str1.value; str1.value = str2.value; str2.value = temp; System.out.println("After swap:"); System.out.println("str1: " + str1.value); System.out.println("str2: " + str2.value); } } class StringWrapper { String value; StringWrapper(String value) { this.value = value; } }
Output:
Using Temporary Variable: Before swap: str1: Hello str2: World After swap: str1: World str2: Hello Using Concatenation and Substring: Before swap: str1: Hello str2: World After swap: str1: World str2: Hello Using Wrapper Class: Before swap: str1: Hello str2: World After swap: str1: World str2: Hello
5. Conclusion
Swapping two strings in Java can be accomplished in several ways, each with its own advantages. Using a temporary variable is straightforward and easy to understand, while concatenation and substring manipulation provide an alternative approach without additional variables. Using a wrapper class simulates pass-by-reference behavior, allowing swaps within methods.
By understanding these different methods, you can choose the one that best fits your needs and coding style. Happy coding!
Comments
Post a Comment
Leave Comment