How do I swap two string variables in Java without using a third variable, i.e. the temp variable?
String a = \"one\" String b = \"two\" String temp = null; temp
The simplest way is given below:
String a = "one"; String b = "two"; System.out.println("Before swap: " a + " " + b); int len = a.length(); a = a + b; b = a.substring(0, len); a = a.substring(len); System.out.println("After swap: " a + " " + b);