Java Program to Swap Two Numbers
Last Updated :
11 Oct, 2025
Problem Statement: Given two integers m and n. The goal is simply to swap their values in the memory block and write the java code demonstrating approaches.
Illustration:
Input : m=9, n=5
Output : m=5, n=9
Input : m=15, n=5
Output : m=5, n=15
Here 'm' and 'n' are integer value

Approach 1: Using a Temporary Variable
- A temporary memory cell is created to hold one value during the swap.
- The value in the temporary variable is assigned to the other variable after swapping.
- The temporary variable acts as a helper and does not appear in the final output.
Java
public class GFG{
public static void main(String[] args){
int m = 9, n = 5;
System.out.println("Before swapping: m = " + m + ", n = " + n);
int temp = m;
m = n;
n = temp;
System.out.println("After swapping: m = " + m + ", n = " + n);
}
}
OutputBefore swapping: m = 9, n = 5
After swapping: m = 5, n = 9
Note: This swap only affects local copies in memory. If you pass m and n to a method, the original variables remain unchanged due to Java's pass-by-value.
Approach 2: Using Arithmetic (Sum and Difference)
Algorithms: There are 3 standard steps as listed below:
- Subtract the second number from the first and store the result in the first variable.
- Add both numbers and store the result in the second variable.
- Subtract the new first variable from the second to get the original first number in the first variable.
Java
public class GFG{
public static void main(String[] args){
int m = 9, n = 5;
System.out.println("Before swapping: m = " + m + ", n = " + n);
m = m + n;
n = m - n;
m = m - n;
System.out.println("After swapping: m = " + m + ", n = " + n);
}
}
OutputBefore swapping: m = 9, n = 5
After swapping: m = 5, n = 9
Note: This method avoids extra variables but can cause overflow with very large numbers.
Approach 3: Using Bitwise XOR Operator
- Operates on individual bits of integer types (char, short, int, etc.).
- Returns 1 for each bit position where the corresponding bits of the two operands are different; otherwise returns 0.
- Commonly used in swapping numbers without a temporary variable and in bit manipulation tasks.
Illustration:
a = 5 -> 0101 (binary)
b = 7 -> 0111 (binary)
XOR Operation: a ^ b
0101
^ 0111
------
0010 -> 2 (decimal)
Using XOR twice can swap two numbers efficiently without extra memory:
a = a ^ b;
b = a ^ b;
a = a ^ b;
Java
public class GFG {
public static void main(String[] args){
int m = 9, n = 5;
System.out.println("Before swapping: m = " + m
+ ", n = " + n);
// Step 1: XOR m and n, store result in m
m = m ^ n;
// Step 2: XOR new m with n to get original m in n
n = m ^ n;
// Step 3: XOR new m with new n to
// get original n in m
m = m ^ n;
System.out.println("After swapping: m = " + m
+ ", n = " + n);
}
}
OutputBefore swapping: m = 9, n = 5
After swapping: m = 5, n = 9
How do we swap using a function and reflect the changes outside?
The previous methods only swaps local copies. To swap numbers in a method and reflect changes outside, we use an array.
Java
public class SwapUsingArray {
public static void main(String[] args){
// Initialize numbers in an array
int[] nums = { 9, 5 };
System.out.println("Before swapping: m = " + nums[0]
+ ", n = " + nums[1]);
// Call the method to swap the numbers
swapArray(nums);
// Print the numbers after swapping
System.out.println(
"After swapping using array: m = " + nums[0]
+ ", n = " + nums[1]);
}
// Method to swap numbers in an array
public static void swapArray(int[] arr){
// Temporary variable to hold the first value
int temp = arr[0];
// Assign second value to first
arr[0] = arr[1];
// Assign temporary value to second
arr[1] = temp;
}
}
OutputBefore swapping: m = 9, n = 5
After swapping using array: m = 5, n = 9
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java