Java Program to Swap Two Numbers
To swap two numbers in Java, use a temporary variable like
temp = a; a = b; b = temp; to exchange their values.Examples
Inputa = 5, b = 10
OutputAfter swap: a = 10, b = 5
Inputa = -3, b = 7
OutputAfter swap: a = 7, b = -3
Inputa = 0, b = 0
OutputAfter swap: a = 0, b = 0
How to Think About It
To swap two numbers, think of holding one number temporarily so you don't lose it when you overwrite its variable. Use a temporary container to store one number, then assign the second number to the first variable, and finally put the stored number into the second variable.
Algorithm
1
Get the first number and store it in variable a2
Get the second number and store it in variable b3
Create a temporary variable temp and assign it the value of a4
Assign the value of b to a5
Assign the value of temp to b6
Print the swapped values of a and bCode
java
public class SwapNumbers { public static void main(String[] args) { int a = 5; int b = 10; int temp; temp = a; a = b; b = temp; System.out.println("After swap: a = " + a + ", b = " + b); } }
Output
After swap: a = 10, b = 5
Dry Run
Let's trace swapping a=5 and b=10 through the code
1
Initial values
a = 5, b = 10
2
Store a in temp
temp = 5
3
Assign b to a
a = 10
4
Assign temp to b
b = 5
5
Print swapped values
a = 10, b = 5
| Step | a | b | temp |
|---|---|---|---|
| Initial | 5 | 10 | undefined |
| Store a in temp | 5 | 10 | 5 |
| Assign b to a | 10 | 10 | 5 |
| Assign temp to b | 10 | 5 | 5 |
| Print result | 10 | 5 | 5 |
Why This Works
Step 1: Use a temporary variable
We use temp to hold the value of a so it is not lost when a is overwritten.
Step 2: Assign second number to first
We assign the value of b to a, so now a has the second number.
Step 3: Assign temp to second number
We assign the stored value in temp to b, completing the swap.
Alternative Approaches
Swap without temporary variable using addition and subtraction
java
public class SwapNumbers { public static void main(String[] args) { int a = 5, b = 10; a = a + b; b = a - b; a = a - b; System.out.println("After swap: a = " + a + ", b = " + b); } }
This method avoids extra memory but can cause overflow with large numbers.
Swap using XOR bitwise operator
java
public class SwapNumbers { public static void main(String[] args) { int a = 5, b = 10; a = a ^ b; b = a ^ b; a = a ^ b; System.out.println("After swap: a = " + a + ", b = " + b); } }
This method uses bitwise operations and no extra memory but is less readable.
Complexity: O(1) time, O(1) space
Time Complexity
Swapping two numbers takes a fixed number of steps regardless of input size, so it is O(1).
Space Complexity
Only a few variables are used, so space complexity is O(1).
Which Approach is Fastest?
All methods run in constant time; using a temporary variable is simplest and safest, while arithmetic or XOR methods avoid extra variables but can be less clear or risky.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Temporary variable | O(1) | O(1) | Clarity and safety |
| Addition and subtraction | O(1) | O(1) | Avoiding extra variable but risk overflow |
| XOR bitwise | O(1) | O(1) | Low-level operations, no extra memory |
Always use a temporary variable to swap numbers safely and clearly.
Forgetting to use a temporary variable causes one value to be overwritten and lost.