Java – Swapping 2 Strings without Third variable

In this article, we will discuss how to swap 2 String variables using third variables and without using 3rd variable

Swapping 2 String variables:

  1. Without using third String variable
  2. Using third String variable

1. Swapping 2 Strings w/o using Third Variable

  • First, concatenate 2 Strings (str1 + str2) and assign it to 1st String variable (str1) using + (plus) operator
  • Next, get sub-string from the concatenated String from 0th index till difference of 2 Strings length and assign it to 2nd String str2
  • Finally, get another sub-string from the concatenated String starting from length of new assigned String str2 till the end
  • We will print both String before & after swapping String variables

SwapTwoStringsWithoutThirdVariable.java

package in.bench.resources.string.operation;

public class SwapTwoStringsWithoutThirdVariable {

	public static void main(String[] args) {

		// String variables
		String str1 = "Java";
		String str2 = "World";


		// print before swapping
		System.out.println("Before swapping 2 Strings :- ");
		System.out.println("String 1 = " + str1);
		System.out.println("String 2 = " + str2);


		// concatenate 2 Strings
		str1 = str1 + str2; // 4 + 5 = 9


		// swap 1st String using String.substring(start, end)
		str2 = str1.substring(0, str1.length() - str2.length()); // 0, 9


		// swap 2nd String using String.substring(start)
		str1 = str1.substring(str2.length()); // 4


		// print after swapping
		System.out.println("\n\nAfter swapping 2 Strings :- ");
		System.out.println("String 1 = " + str1);
		System.out.println("String 2 = " + str2);
	}
}

Output:

Before swapping 2 Strings :- 
String 1 = Java
String 2 = World


After swapping 2 Strings :- 
String 1 = World
String 2 = Java

2. Swapping 2 Strings using Third Variable

  • Declare a temp variable
  • First, assign 1st String value to temp variable
  • Second, assign 2nd String value to 1st String variable
  • Third, assign temp String value to 2nd String variable
  • We will print both String before & after swapping String variables

SwapTwoStrings.java

package in.bench.resources.string.operation;

public class SwapTwoStrings {

	public static void main(String[] args) {

		// String variables
		String str1 = "Java";
		String str2 = "World";
		String temp = null;


		// print before swapping
		System.out.println("Before swapping 2 Strings :- ");
		System.out.println("String 1 = " + str1);
		System.out.println("String 2 = " + str2);


		// swapping using temp variable
		temp = str1;
		str1 = str2;
		str2 = temp;


		// print after swapping
		System.out.println("\n\nAfter swapping 2 Strings :- ");
		System.out.println("String 1 = " + str1);
		System.out.println("String 2 = " + str2);
	}
}

Output:

Before swapping 2 Strings :- 
String 1 = Java
String 2 = World


After swapping 2 Strings :- 
String 1 = World
String 2 = Java

Happy Coding !!
Happy Learning !!

Java 8 – Count and print number of Vowels and Consonants in a String
Java 8 – Reverse complete String using Stream and Collectors