program to swap two strings without using third variable in java

Here we will see a program to swap two strings without using third variable in java.

public class SwapTwoString {
public static void main(String[] args) {
	 String string1 = "my";  
     String string2 = "name";  
     System.out.println("Before swapping of string : " + string1 + " " + string2);  
     string1 = string1 + string2;  
     string2 = string1.substring(0, string1.length() - string2.length());  
     string1 = string1.substring(string2.length());  
     System.out.println("Before swapping of string : " + string1 + " " + string2);  
}
}

 

Output is –


Before swapping of string : my name
After swapping of string : name my