📘 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
In this quick article, we will see how to write a Java program to swap two strings with or without using a third variable. First, we will see how to write a Java program to swap two strings using a third variable, and then we will see how to write a Java program to swap two strings without using a third variable.
Java Program to Swap Two Strings With a Third Variable
Using a third variable is the simplest and most intuitive way to swap two strings. Here's how you can do it:
Java Code
package com.java.tutorials.programs; public class SwapTwoStrings { public static void main(String[] args) { String s1 = "java"; String s2 = "guides"; System.out.println("Before swapping two strings:"); System.out.println("s1 => " + s1); System.out.println("s2 => " + s2); // Using a third variable to swap the strings String temp; temp = s1; // store s1 in temp s1 = s2; // assign s2 to s1 s2 = temp; // assign temp (original s1) to s2 System.out.println("After swapping two strings:"); System.out.println("s1 => " + s1); System.out.println("s2 => " + s2); } }
Output
Before swapping two strings: s1 => java s2 => guides After swapping two strings: s1 => guides s2 => java
Explanation
- Initial Values: The strings
s1
ands2
are initially assigned the values"java"
and"guides"
, respectively. - Swapping:
- The value of
s1
is stored in a temporary variabletemp
. - The value of
s2
is then assigned tos1
. - Finally, the value stored in
temp
(which is the original value ofs1
) is assigned tos2
.
- The value of
- Result: After swapping,
s1
becomes"guides"
ands2
becomes"java"
.
Java Program to Swap Two Strings Without Using a Third Variable
Swapping two strings without using a third variable can be done by utilizing string concatenation and substring operations. Here's how:
Java Code
package com.java.tutorials.programs; /** * Java Program to Swap Two Strings Without Using a Third Variable * @author Ramesh Fadatare * */ public class SwapTwoStrings { public static void main(String[] args) { String s1 = "java"; String s2 = "guides"; System.out.println("Before swapping two strings:"); System.out.println("s1 => " + s1); System.out.println("s2 => " + s2); // Step 1: Concatenate s1 and s2 and store the result in s1 s1 = s1 + s2; // "javaguides" // Step 2: Extract the initial value of s1 from the concatenated string s2 = s1.substring(0, s1.length() - s2.length()); // "java" // Step 3: Extract the initial value of s2 from the concatenated string s1 = s1.substring(s2.length()); // "guides" System.out.println("After swapping two strings:"); System.out.println("s1 => " + s1); System.out.println("s2 => " + s2); } }
Output
Before swapping two strings: s1 => java s2 => guides After swapping two strings: s1 => guides s2 => java
Explanation
- Initial Values: The strings
s1
ands2
are initially assigned the values"java"
and"guides"
, respectively. - Swapping Without Third Variable:
- Step 1: Concatenate
s1
ands2
, and store the result ins1
. Now,s1
contains"javaguides"
. - Step 2: Extract the original value of
s1
(which is"java"
) by taking the substring ofs1
from the beginning to the length of the originals1
. This value is now stored ins2
. - Step 3: Extract the original value of
s2
(which is"guides"
) by taking the substring ofs1
starting from the length of the originals1
to the end. This value is now stored ins1
.
- Step 1: Concatenate
- Result: After swapping,
s1
becomes"guides"
ands2
becomes"java"
.
Conclusion
These two approaches demonstrate how to swap two strings in Java, both with and without using a third variable. The first method, using a temporary variable, is straightforward and easy to understand. The second method, which avoids using a third variable, is a bit more advanced and showcases the power of string manipulation in Java.
Comments
Post a Comment
Leave Comment