How to String swapping without third variable in java ?

Given two variables first one is str and second is str1 swapping these variables without third variable.

In using the substring method of String and user enter input using Scanner.

String swapping without third variable in java

import java.util.Scanner;
class str
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println(” Enter first word “);
String str=sc.nextLine();
System.out.println(” Enter Second word “);
String str1=sc.nextLine();
str=str+str1;
str1 =str.substring(0,str.length()-str1.length());
str=str.substring(str1.length(),str.length());
System.out.println(str + ” ” + str1);
}
}

Amandeep kaur: Amandeep Kaur is passionate about programming and is working on Android development from her school days. She has worked on many open source projects and now part of Hoodu Digital solutions as a Android developer.