Swapping of Two Numbers in Java

There are many different methods for swapping of two numbers in Java. Swapping of two numbers means to exchange the values of the variables with each other.

Before,
X=10 and Y=20
Then after swapping
X=20 and Y=10

First we will learn how to swap two numbers using a third variable, and then we will see different methods to swap two numbers without using third variable in java.

Java program to swap two numbers using third variable

Procedure:-
1) Take two numbers. For example:- int x = 10; int y = 20
2) declare a temporary/third variable of same data type, int temp;
3) Assign x value to third variable, temp = x;
4) Now, assign y value to x variable, x = y
5) Finally, assign temp value to y variable, y = temp;

int x = 10, y = 20;
int temp = x;
x = y;
y = x;

Execution:-
x = 10, y = 20
temp = x => temp = 10 (temp is hold value of x)
x = y=> x = 20 ( now, x is holding value of y)
y = temp => y = 10 (now, y is holding previous value of x)

public class Swap {

  public static void main(String[] args) {

	int x=10, y=20;
	int temp;

	temp = x;
	x = y;
	y = temp;

	System.out.println("Values After Swapping,");
	System.out.println("x="+x+"\t y="+y);
  }
}

Output:-

Values After Swapping,
x=20 y=10

The same program through reading input from the end-user,

import java.util.Scanner;

public class Swap {

   public static void main(String[] args) {

      // declare variables
      int x, y;
      int temp;

      // create Scanner class object
      // to read input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter two numbers: ");
      x = scan.nextInt();
      y = scan.nextInt();

      // displaying values before swapping
      System.out.println("Values before Swapping,");
      System.out.println("x="+x+"\t y="+y);

      // swap the numbers
      temp = x;
      x = y;
      y = temp;

      // displaying values after swapping
      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);

      // close Scanner class object
      scan.close();
   }
}

Output:-

Enter two numbers: 50 90
Values before Swapping,
x=50 y=90
Values After Swapping,
x=90 y=50

Swap two numbers without using third variable in Java

Now, we will discuss different ways to swap two numbers without using third variable in Java. We can use a combination of operators like (+, -), (*,/) and XOR operator.

Swapping of two numbers in Java using + and – operator

public class Swap {

   public static void main(String[] args) {

      int x=10, y=20;

      x = x + y;
      y = x - y;
      x = x - y;

      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);
   }
}

Output:-

Values After Swapping,
x=20 y=10

Execution:-
Before,
x=10, y=20
x = x+y => x = 10 + 20 = 30 Now, x=30, y=20
y = x-y => y = 30 – 20 = 10 Now, x=30, y=10
x = x-y => x = 30 – 10 = 20 Now, x=20, y=10
Finally,
x=20, y=10

The above swapping code also can be written in a single line as,

x = (x + y) - (y = x);
public class Swap {

   public static void main(String[] args) {

      int x=10, y=20;

      x = (x + y) - (y = x);

      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);
   }
}

Note:- This method works well, but it may cause an arithmetic overflow when swapping large numbers.

Swap two numbers in Java using * and / operator

public class Swap {

   public static void main(String[] args) {

      int x=10, y=20;

      x = x * y;
      y = x / y;
      x = x / y;

      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);
   }
}

Output:-

Values After Swapping,
x=20 y=10

Execution:-
Values before swapping,
x = 10, y = 20
x=x*y => x = 10 * 20 = 200 Now, x=200, y=20
y=x/y => y = 200 / 20 = 10 Now, x=200, y=10
x=x/y => x = 200 / 10 = 20 Now, x=20, y=10
Values after swapping,
x = 20, y = 10

Above swapping code also can be written in a single line as,

x = ( x*y ) / (y = x);
public class Swap {

   public static void main(String[] args) {

      int x=10, y=20;

      x = ( x*y ) / (y = x);

      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);
   }
}

Note:- This method will not work if any variable value is zero. Because the product becomes zero. It may also cause an arithmetic overflow when multiplying large numbers.

Swap two numbers using Bitwise XOR operator (^)

We can swap two numbers using the XOR operator. The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.

public class Swap {

   public static void main(String[] args) {

      int x=10, y=20;

      x = x ^ y;
      y = x ^ y;
      x = x ^ y;

      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);
   }
}

Output:-

Values After Swapping,
x=20 y=10

This method is a bit easy because it uses only the bitwise operator. It also can be written as,

x ^= y;
y ^= x;
x ^= y;

Execution:-

Let At starting, x=12 and y=20 so,
x=12=1100(in binary) and y=20=10100(in binary)
x = x^y ; 01100^10100=11000=24, now x=24, y=20
y = x^y ; 11000^10100=01100=12, now x=24, y=12
x = x^y ; 11000^01100=10100=20, now x=20, y=12
Finally, x=20, y=12

Swapping of two numbers in Java using + and – operator in a slightly different way

x = y - x;
y = y - x;
x = y + x;

Note that the corresponding method does not work with * and / operators.

public class Swap {

   public static void main(String[] args) {

      int x=10, y=20;

      x = y - x;
      y = y - x;
      x = y + x;

      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);
   }
}

Output:-

Values After Swapping,
x=20 y=10

Execution:-
Before swapping,
x=10, y=20
x=y-x => x=20-10=10 Now, x=10, y=20
y=y-x => y=20-10=10 Now, x=10, y=10
x=y+x => x=10+10=20 Now, x=20, y=10

Swap two numbers using bitwise complement operator (~) and arithmetic operators (+,-)

a = b - ~a - 1;
b = a + ~b + 1;
a = a + ~b + 1;
public class Swap {

   public static void main(String[] args) {

      int x=10, y=20;

      x = y - (~x) - 1;
      y = x + (~y) + 1;
      x = x + (~y) + 1;

      System.out.println("Values After Swapping,");
      System.out.println("x="+x+"\t y="+y);
   }
}

Output:-

Values After Swapping,
x=20 y=10

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *