This Java program swaps the values of two strings without using a third variable. It does so by concatenating the two strings, then extracting the original values by using the substring() method. Here's a breakdown of how the swapping works:
class Swap_Without_Third { public static void main(String[] args) { String s1 = "Tutor"; String s2 = "Joes"; System.out.println("Before Swap String 1 :"+s1); System.out.println("Before Swap String 2 :"+s2); s1 = s1 + s2; s2 = s1.substring(0, s1.length() - s2.length()); s1 = s1.substring(s2.length()); display(s1, s2); } private static final void display(String s1, String s2) { System.out.println("After Swap String 1 :"+s1); System.out.println("After Swap String 2 :"+s2); } }
Before Swap String 1 :Tutor Before Swap String 2 :Joes After Swap String 1 :Joes After Swap String 2 :Tutor
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions