Swap Two Numbers in Java
What you’ll learn
- What swapping means and why
tempis the safest method. - Two Java implementations: with temp and without temp.
- How to avoid pitfalls like arithmetic overflow in no-temp swaps.
- A live preview to visualize before/after values.
Overview
Swapping exchanges values of two variables: if a=5, b=10, after swap a=10, b=5. A temporary variable prevents value loss and is the recommended interview approach.
Two Java programs
Temp-variable and arithmetic no-temp methods.
Live preview
Enter values and watch before/after swap output.
Interview tip
Use temp swap first, mention no-temp method as extra.
Prerequisites
Variables, assignment, and basic integer arithmetic in Java.
- Understand that assignment overwrites current variable values.
- Know overflow can happen with arithmetic operations on large integers.
What does swapping do?
Swapping makes first variable hold the old second value, and second variable hold the old first value.
Without temporary storage (or a safe trick), one value can be lost during assignment.
Why temp is safest
The temp variable preserves one original value before overwriting either variable. It is clear, safe, and avoids arithmetic overflow pitfalls.
Trace on paper
Live preview
Enter two integers and press Swap to see before/after lines.
Algorithm
Copy first into temp
temp = a.
Move second into first
a = b.
Restore temp into second
b = temp.
📜 Pseudocode
procedure swap(a, b):
temp = a
a = b
b = tempSwap using temp variable
Most readable and recommended method for beginners.
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
int temp = a;
a = b;
b = temp;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}Swap without temp variable
Uses arithmetic operations. Works, but can overflow for very large values.
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}Other tricks (after temp method)
Arithmetic no-temp swap is shorter but can overflow on large values.
Default choice: temp-variable swap is clearer and safer for production code.
Interview: write temp method first, mention alternatives briefly.
❓ FAQ
🔄 Input / output examples
| Before | After |
|---|---|
a=5,b=10 | a=10,b=5 |
a=7,b=7 | a=7,b=7 |
Edge cases
Most mistakes are from overwriting one value too early or using overflow-prone arithmetic swap.
No visible change
If both values are same, result remains same and is still correct.
Do not start with a = b
You lose original a unless it is stored first.
Arithmetic swap
a=a+b can overflow for large integers.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| Swap with temp variable | O(1) | O(1) |
| Swap without temp (arithmetic) | O(1) | O(1) |
Summary
- Best practice: temp-variable swap is safe and clear.
- Alternative: arithmetic method avoids temp but can overflow.
- Complexity: both are constant time and constant space.
Swapping is a basic operation used in many sorting algorithms like bubble sort and selection sort.
8 people found this page helpful
