Java Program to Swap two Variables
Last Updated :
12 Jul, 2025
Given two numbers x and y, we need to swap their values. In this article, we will learn the Swapping of two numbers in Java.
Examples of Swapping
Input : x = 10, y = 20;
Output : x = 20, y = 10
Input : x = 213, y = 109
Output : x = 109, y = 213

Steps to Swap Two Numbers in Java
Below are the simple steps we follow:
- Assign x to a temp variable: temp = x
- Assign y to x: x = y
- Assign temp to y: y = temp
Methods for Swapping of Two Numbers in Java
1. Using Three Variables
Below is the implementation of the above method:
Java
// Java program to Swap
// Two variables
// Driver Class
public class GfG {
// main function
public static void main(String[] args)
{
int x = 100, y = 200;
System.out.println("Before Swap");
System.out.println("x = " + x);
System.out.println("y = " + y);
// Swapping using three
// Variables
int temp = x;
x = y;
y = temp;
System.out.println("After swap");
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
OutputBefore Swap
x = 100
y = 200
After swap
x = 200
y = 100
2. Using Two Variables
Below is the implementation of the above method:
Java
// Java Program to Swap
// Two Numbers using Two
// Variables
import java.io.*;
// Driver Class
public class SwapVariables {
// main function
public static void main(String[] args)
{
int a = 5;
int b = 10;
// print statement
System.out.println("Before swapping, a = " + a
+ " and b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping, a = " + a
+ " and b = " + b);
}
}
OutputBefore swapping, a = 5 and b = 10
After swapping, a = 10 and b = 5
3. Using Two Variables (Single Statement)
Below is the implementation of the above method:
Java
// Java Program to Swap Two
// Numbers using Two variables
// Single Statement
import java.io.*;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
Integer a = 10;
Integer b = 20;
System.out.println("Before swapping, a = " + a
+ " and b = " + b);
// Swapping a and b using
// below statement
b = a + b - (a = b);
System.out.println("Before swapping, a = " + a
+ " and b = " + b);
}
}
OutputBefore swapping, a = 10 and b = 20
Before swapping, a = 20 and b = 10
4.Using XOR
We can swap the numbers using XOR operator as shown below.
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int a = 10;
int b = 20;
System.out.println("Before swapping, a = " + a + " and b = " + b);
// Swapping a and b using XOR
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("After swapping, a = " + a + " and b = " + b);
}
}
OutputBefore swapping, a = 10 and b = 20
After swapping, a = 20 and b = 10
Time Complexity:
The time complexity of this XOR-based swap operation is constant, O(1), because it doesn't depend on the size of the numbers being swapped or any iterative operations. It involves a few XOR and assignment operations, which are executed in constant time.
Space Complexity:
The space complexity is also O(1) because it doesn't require any additional memory allocation that scales with the size of the input. Regardless of the values of a and b, the memory usage remains constant because it only uses a few extra integer variables to perform the swapping, and these variables don't depend on the input size.
References
Java Program to Swap Two Numbers
Similar Reads
Java Program to Swap two Strings Without Using any Third Variable Given two string variables, a and b, your task is to write a Java Program to swap these variables without using any temporary or third variable. Use of library methods is allowed. Examples: Input: a = "Hello" b = "World" Output: Strings before swap: a = Hello and b = World Strings after swap: a = Wo
4 min read
Swap Two Variables in One Line We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function?1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write "x, y = y, x".2) C/C++: Belo
4 min read
Java Program to Swap the Elements of Vector The swap() method of java.util.Collections class is used to swap the elements at the specified positions in the specified list. If the specified positions are equal, invoking this method leaves the list unchanged. Syntax: public static void swap(List list, int i, int j) Parameters: This method takes
2 min read
Javascript program to swap two numbers without using temporary variable To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithme
6 min read
Java Variables In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated.Key Components of Variables in Java:A variable in Java has three components, which are listed below:Data Type: Defines the kind
9 min read
Swap Two Numbers Without Using Third Variable Given two variables a and y, swap two variables without using a third variable. Examples: Input: a = 2, b = 3Output: a = 3, b = 2Input: a = 20, b = 0Output: a = 0, b = 20Input: a = 10, b = 10Output: a = 10, b = 10Table of ContentUsing Arithmetic OperatorsUsing Bitwise XORBuilt-in SwapUsing Arithmeti
6 min read