Swapping Two Numbers Using Arithmetic Operators + and –
In this approach, we use addition and subtraction to swap the values without a temporary variable. This method works by manipulating the values directly.
Example:
int a = 5;
int b = 10;
a = a + b; // a now becomes 15 (5 + 10)
b = a - b; // b now becomes 5 (15 - 10)
a = a - b; // a now becomes 10 (15 - 5)
System.out.println("a = " + a + ", b = " + b); // Output: a = 10, b = 5
This approach is memory-efficient but requires caution when handling large numbers, as it may lead to integer overflow.
Swapping Two Numbers Using Arithmetic Operators * and /
This method uses multiplication and division to swap the values. This technique is suitable only when both numbers are non-zero, as dividing by zero will cause an error.
Example:
int a = 5;
int b = 10;
a = a * b; // a now becomes 50 (5 * 10)
b = a / b; // b now becomes 5 (50 / 10)
a = a / b; // a now becomes 10 (50 / 5)
System.out.println("a = " + a + ", b = " + b); // Output: a = 10, b = 5
This approach is also memory-efficient but should be used cautiously with zero or very large values to avoid division errors or overflow.
Swapping Two Numbers Using Bitwise XOR Operator ^
The XOR method swaps values without any additional memory or the risk of overflow. It’s a bitwise technique often used in low-level programming for efficient memory usage.
Example:
int a = 5;
int b = 10;
a = a ^ b; // a now becomes 15 (5 XOR 10)
b = a ^ b; // b now becomes 5 (15 XOR 10)
a = a ^ b; // a now becomes 10 (15 XOR 5)
System.out.println("a = " + a + ", b = " + b); // Output: a = 10, b = 5
Using the XOR operator is efficient and avoids extra memory usage or overflow issues, making it a preferred method for low-level applications and performance-sensitive code.
Swapping Two Numbers Using extra space
With the extra space, we can easily swap two numbers. Variable num1, num2, temp act as containers where we can store numbers. The following steps are used in the program for swapping.
Two numbers will be stored in the variable num1 and num2. Now for swapping two numbers, first, we hold the num1 value in temp. In the temp variable, a copy will be saved. Now, store the value of num2 in the num1 variable. At last, copy the value stored in the temp variable to the num1 variable.
Java
public class swap {
public static void main(String[] args) {
//Program for swapping two numbers
int num1 = 4;
int num2 = 5;
//Before Swapping
System.out.println("~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~");
System.out.println("Number 1 : "+ num1);
System.out.println("Number 2 : "+ num2);
System.out.println();
int temp = num1;
num1 = num2;
num2 = temp;
//After Swapping
System.out.println("~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~");
System.out.println("Number 1 : "+ num1);
System.out.println("Number 2 : "+ num2);
scn.close();
}
}
You can also try this code with Online Java Compiler
Run Code
Output :
~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~
Number 1 : 4
Number 2 : 5
~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~
Number 1 : 5
Number 2 : 4
Numbers 4 and 5 are numbers stored in the variables num1 and num2. Then, swapping two numbers is shown in the above output by following the steps.
Try it by yourself on Java Online Compiler.
Swapping Two Numbers Without using extra space
We can perform swapping two numbers without using extra space. It will be better than the previous program because we will save space here. The code is written below.
Java
public class swap {
public static void main(String[] args) {
//Program for swapping two numbers
int num1 = 10;
int num2 = 20;
//Before Swapping
System.out.println("~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~");
System.out.println("Number 1 : "+ num1);
System.out.println("Number 2 : "+ num2);
System.out.println();
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
//After Swapping
System.out.println("~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~");
System.out.println("Number 1 : "+ num1);
System.out.println("Number 2 : "+ num2);
scn.close();
}
}
You can also try this code with Online Java Compiler
Run Code
Output :
~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~
Number 1 : 10
Number 2 : 20
~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~
Number 1 : 20
Number 2 : 10
Numbers 10 and 20 are stored in num1 and num2, respectively. Now,
Step 1: num1 = num1+num2 , so, num1 will now become 30(10+20)
Step 2: num2 = num1 - num2 , so, num2 = 30 - 20 = 10. It means now num2 will have value 10.
Step 3: num1 = num1 - num2 , so, num1 = 30 - 10 = 20. It means the new value of num1 is 20.
By following the above steps, swapping two numbers is done. The new value of num1 is 20, and num2 is 10.
Also check out Addition of Two Numbers in Java here.
Also see, Duck Number in Java
Frequently Asked Questions
Can swapping two numbers be performed without using user input?
Yes, you have to initialize the variable with some value instead of using a scanner object to input from the user.
Can swapping two numbers be performed when the numbers are float or double?
Yes, swapping can be done even when the numbers are of float or double type.
How can we take input in the program if we want to?
Scanner is a class in Java that is used to take input from the user. It can input primitive data types like int, float, string, or double.
Also Read - Strong number in c
Conclusion
In this article, we have extensively discussed the swapping of two numbers and its implementation in Java. Swapping two numbers is a simple program where two numbers are exchanged. It can be done in two ways, using extra space and without using extra space. It will perform more calculations without using additional variables than when extra space is used. But both are simple to apply.
We hope that this blog has helped you enhance your knowledge regarding swapping of two numbers and if you would like to learn more, check out our articles on Swapping of two numbers in C++. Do upvote our blog to help other ninjas grow. Happy Coding!