Java Program to Swap Two Numbers

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Exchanging the values stored in two variables is known as swapping. Suppose we have two variables, var1 and var2, and we need to change the value of var2 to the value of var1 and vice versa. This process is known as swapping the values. There are multiple ways to swap the values of two variables. It can be swapped using a temporary variable or simple mathematical operations such as addition and subtraction or multiplication and division or bitwise XOR. In this article, we will look at the different ways to swap the values of two variables using Java.

Example 1: Swap Two Numbers Using Temporary Variable

Let us think of a real-life example. Suppose you have a box containing a red ball and a box containing a black ball. You want to swap the balls in the two boxes.

How will you do this?

You can take the help of a temporary box. You can keep the black ball in the temporary box and keep the red ball in the box which contained the black ball. Later you can take the black ball from the temporary box and keep it in the box which contained the red ball initially.

Swap Two Numbers in Java using Temporary Variable

How can we map this solution to a function?

We will create a temporary variable and assign the value of the first variable to the temporary variable. Similar to how we took a temporary box in the box and ball example. Later we will assign the value of the second variable to the first variable and then we assign the value of the temporary variable to the second variable, Just like how we changed the position of the balls in the box.

Let us look at the code:

Output:

As we can see in the output, the value of the first number and the second number is swapped using a temporary variable.

Example 2: Swap Two Numbers without Using a Temporary Variable

Previously, we saw the swapping of two numbers in Java using a temporary variable. Now let us see how swapping two numbers in Java without using a temporary variable.

Using Arithmetic Addition and Subtraction

We saw how to swap two numbers using a temporary variable. We can swap two numbers without using a temporary variable as well, i.e., by simple mathematics. Let us look at the code, and later we will understand the logic behind the code.

Code:

Output:

Explanation

Now that we have seen the code and the output, let us understand what happened in the code that led to the swapping of two numbers. The swapping of numbers occurred in the 10, 11, and 12th lines.

  • In the line 11, a = a + b, a is assigned the value of a + b, i.e., the sum of given numbers. In this case, a = 10 + 20, i.e., a = 30.
  • In the next step, i.e., b = a - b, we take the difference between the sum (of a and b) and b. Since b is subtracted from the sum of a and b, we got the value of a. Hence, using b = a - b, the original value of a is assigned to b. In this case, we get b = 30 - 20, i.e., b = 10.
  • Till now, we have assigned the value of a in b but we are yet to assign the value of b in a. This is also simple to do.
  • If we subtract the original value of a from the sum, then we will end up with the value of b.
  • So, with a = a - b, the original value of a (currently stored in b) is subtracted from the sum (currently stored in a). Hence, now a contains the original value of b. So, in the last step, i.e., a = a - b, we get a = 30 - 10, i.e., a = 20.

As you can see, we just swapped the two numbers without using a temporary variable.

Note: If the value of a and b are too large, this approach may fail due to arithmetic overflow as addition may go out of the integer range.

Using Arithmetic Multiplication and Division

Output:

Explanation

Like the previous method, even though this method is implemented, we use multiplication and division to swap two numbers that we can easily understand from the above code.

Note: The arithmetic multiplication and division method to swap two numbers work fine with non-zero numbers. But this approach won't work if any one of the numbers is 0 because the product becomes 0 (in line 11) irrespective of the other number.

Using Bitwise XOR Operator

The XOR of two numbers a and b returns a number that has all the bits as 1 wherever bits of a and b differ.

Example: 5 is represented as 101 in binary, 4 as 100 in binary. The XOR of 5 and 4 is 001, i.e., 1.

DecimalBinary
Number5101
Number4100
Bitwise XOR1001

Let us see how can we use it to swap two numbers.

Output:

Explanation

In this method, we made use of bitwise XOR operator. Bitwise XOR has two interesting properties:

  • x ^ x = 0
  • x ^ 0 = x

^ operator is used to denote xor operation.

  • So, in line 10, a = a ^ b, the value of a is changed to a ^ b.
  • In line 11, b = a ^ b. So, b becomes
  • Thus, the value of a is now stored in b.
  • And, in line 13, a = a ^ b. So, a becomes
  • Thus, the value of b is now stored in a as well.

In this way, we successfully swapped the numbers a and b using the bitwise XOR operator.

Note: The above method to swap two numbers works fine with non-zero numbers. But this approach won't work if any one of the numbers is 0 because the XOR will be equal to the non-zero number and the same will be assigned to both the numbers.

Conclusion

  • Swapping of two numbers in Java can be done using a temporary variable.
  • Simple arithmetic operations such as addition and subtraction or multiplication and division can also be used in swapping two numbers in Java.
  • Bitwise XOR operator can be used in the swapping of two numbers in Java.

See More