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); } } |
Before 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); } } |
Before 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); } } |
Before 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); } } |
Before 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.
Java Program to Swap two Variables
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