Swap two String without using third variable

Swap two String without using third variable . In this demo two string value has been used and without using third variable swapping their value and finally it will show you results. Please see example below: 

public class SwapVariable {

	public static void main(String[] args) {

	// Swaping String
	String value1 = "Java";
	String value2 = "Honk";

	value1 = value1 + value2;
	value2 = value1.substring(0, (value1.length() - value2.length()));
	value1 = value1.substring(value2.length(), value1.length());

	System.out.println("value1 = " + value1);
	System.out.println("value2 = " + value2);

	}

}

 

Output:

Swap two String without using third variable - Using java

That’s it for Swap two String without using third variable

Leave a Reply

Your email address will not be published. Required fields are marked *