Swap two numbers without using third variable in Java

Swapping two numbers without using third or temporary variable is one of the most commonly asked question not just on Java but also on C and C++ tests and interviews. Swapping two numbers or strings is a very easy question till the condition of using only two variable is not applied, lots of freshers failed to solve this swap without using the third variable. Mostly we come out with a solution with just arithmetic operator but if the test level is little up and asks about other approaches of swapping two variables without using third or temp variable, than you have to look over this little program.

Since you are looking for a swap two numbers program in java without using any temp variable, let me tell that this post is a complete resource to learn different approaches on swapping two numbers without using third variable.

We just have to swap (interchange) the 2 numbers, stored in 2 different variables. Don't forget we are not using the third variable.

Example:
INPUT> x=12 & y=53
OUTPUT>x=53 & y=12

as I told you earlier, you may be asked to swap two numbers with different approaches without using any temporary or third variable. This post contains four methods to do so!

Method 1: Swapping Using Arithmetic Operator
In 90% of cases this method strikes first to a fresher while swapping without using third variable.

import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 1
            x=x+y;
            y=x-y;
            x=x-y;
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}

Method 2: Swapping Using Bitwise Operators
Second approach two swap to numbers without using third variable is using XOR bitwise operators.

import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 2
            x = x^y;
            y = x^y;
            x = x^y;
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}

Method 3: Swapping Using Multiplication & Division
Like  Method 1 where we swapped two numbers x and y without using third variable by the simple logic of addition and subtraction, In this method we do the same swapping using multiplication and division logic keeping in mind that we don't have to use third variable.

import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 3
            x = x*y;
            y = x/y;
            x = x/y;
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}

Method 4: Swapping Using 'One Line Logic!'
Now here comes my favorite method to swap two numbers... Its the best method for swapping because only one line logic is required for this.
x = y-x+(y=x);

import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 4
            x = y-x+(y=x);
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}

Swap two numbers without using third variable in Java

Hope we are able to explain you Swap two numbers without using third variable in Java, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).

Please share us on social media if you like the tutorial.