Introduction
Swapping two numbers is one of the most fundamental programming exercises in Java. Swapping means exchanging the values of two variables so that the first variable holds the second variable’s value and the second variable holds the first variable’s value.
For example:
Before Swapping:
First Number = 10
Second Number = 20
After Swapping:
First Number = 20
Second Number = 10
The Java Swap Numbers program is commonly asked in coding interviews because it tests your understanding of variables, arithmetic operators, assignment operators, and programming logic.
In this tutorial, you’ll learn different ways to swap two numbers in Java, including using a temporary variable, arithmetic operators, bitwise XOR, and user input.
What is Swapping?
Swapping is the process of exchanging the values of two variables.
Example:
Before:
a = 5
b = 10
After:
a = 10
b = 5
Swapping is useful in many algorithms such as sorting, searching, and data manipulation.
Java Program to Swap Two Numbers Using a Temporary Variable
Using a temporary variable is the simplest and most recommended approach.
public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
int temp;
System.out.println("Before Swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
temp = a;
a = b;
b = temp;
System.out.println("\nAfter Swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Output
Before Swapping:
b = 20
After Swapping:
b = 10
How the Program Works?
Step 1: Declare Variables
int a = 10;
int b = 20;
These variables store the values to be swapped.
Step 2: Store One Value
temp = a;
The value of a is stored in the temporary variable.
Step 3: Assign Second Value
a = b;
Now a gets the value of b.
Step 4: Assign Temporary Value
b = temp;
Finally, b gets the original value of a.
Java Program to Swap Two Numbers Without Using a Temporary Variable
You can swap two numbers using arithmetic operators.
public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
a = a + b;
b = a - b;
a = a - b;
System.out.println("After Swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Output
After Swapping:
b = 10
Note: This approach may cause integer overflow when very large numbers are used.
Java Program Using Bitwise XOR Operator
The XOR operator can also swap two numbers without a temporary variable.
public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("After Swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Output
After Swapping:
a = 20
b = 10
Java Program Using User Input
import java.util.Scanner;
public class SwapNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
int temp = a;
a = b;
b = temp;
System.out.println("\nAfter Swapping:");
System.out.println("First Number = " + a);
System.out.println("Second Number = " + b);
scanner.close();
}
}
Output
Enter second number: 30
After Swapping:
Second Number = 15
Algorithm
- Read two numbers.
- Store the first number in a temporary variable.
- Assign the second number to the first variable.
- Assign the temporary value to the second variable.
- Display the swapped values.
Flowchart
Start
│
Read a and b
│
temp = a
│
a = b
│
b = temp
│
Display a and b
│
End
Time Complexity
All swapping methods execute a fixed number of operations.
O(1)
Space Complexity
| Method | Space Complexity |
|---|---|
| Using Temporary Variable | O(1) |
| Arithmetic Method | O(1) |
| XOR Method | O(1) |
Common Mistakes
1. Forgetting the Temporary Variable
Incorrect:
a = b;
b = a;
Both variables become the same.
Correct:
temp = a;
a = b;
b = temp;
2. Integer Overflow
Incorrect:
a = a + b;
If a and b are very large, overflow may occur.
Use the temporary variable approach when working with large values.
3. Using XOR with Different Data Types
The XOR method works only with integer types.
4. Not Printing Values Before Swapping
Printing both the original and swapped values makes it easier to verify the program.
5. Forgetting to Close Scanner
Always close the Scanner object after reading user input.
scanner.close();
Real-Life Applications
Swapping values is widely used in:
- Sorting algorithms (Bubble Sort, Selection Sort, Quick Sort)
- Data manipulation
- Memory management
- Searching algorithms
- Competitive programming
- Interview coding questions
Summary
The Java Swap Numbers program is one of the most important beginner-level programming exercises. It introduces fundamental concepts such as variables, assignment, arithmetic operations, and logical thinking.
Among all approaches, using a temporary variable is the most recommended because it is simple, readable, and safe. Although arithmetic and XOR methods eliminate the need for an extra variable, they are generally used for educational purposes and interview discussions rather than production code.