How to swap two numbers in java

Problem Statement: 

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

Approaches:

There are 3 different approaches to exchanging numbers which vary depending on the complexity of space and time.

  1. Creating an auxiliary memory cell in the memory.
  2. Without creating any auxiliary(additional) memory cell
  3. Using exclusive OR (Bitwise XOR) operator

Approaches are described below individually in same order as listed above:

Approach 1: Swapping the Values Using Third Variable

A memory cell will be created in the same type of memory that occupies the same memory in the memory stack area. During execution, it retains a value to replace other values, and once the required execution is completed, its value will be assigned to the second variable that already exists. Once the scope of the variable is three variables, it will be released from the memory unit. This variable is called a temporary variable, sometimes called a catalyst, because the participation in the output is not even tracked, but the execution will stop to produce the desired result seen above.

// Java Program to Swap Two values using third variable
// using temp variable

// Importing generic libraries
import java.util.*;

class GFG {

	// Function to swap two numbers
	// Using temporary variable
	static void swapValuesUsingThirdVariable(int m, int n)
	{
		// Swapping the values
		int temp = m;
		m = n;
		n = temp;
		System.out.println("Value of m is " + m
						+ " and Value of n is " + n);
	}

	// Main driver code
	public static void main(String[] args)
	{
		// Random integerslues
		int m = 9, n = 5;

		// Calling above function to
		// reverse the numbers
		swapValuesUsingThirdVariable(m, n);
	}
}

Output

Value of m is 5 and Value of n is 9

Approach 2  Exchange of values ??without using a third variable using the mathematical concepts of sum and difference.

Algorithms:  There are 3 standard steps as listed below: 

  1. The difference between the second number and the first number is stored in the memory cell where the first number is already stored.
  2. The sum of the two numbers is stored in the second memory cell (number).
  3. The difference between the first and second numbers is calculated and stored in the memory cell where the original value is stored.
 
// Java Program to swap the two values
// without using third variable

// Importing generic Java libraries
import java.util.*;

class GFG {

	// Function to swap valus of two numbers
	// without creating temp variable
	static void swapValuesWithoutUsingThirdVariable(int m,
													int n)
	{
		// Steps as listed in algorithm

		// Difference of 2nd from 1st
		// is stored in first variable
		m = m - n;

		// Sum is stored in second variable
		n = m + n;

		// Difference of 1st from 2nd
		// is replaced in first variable
		m = n - m;

		// Print numbers
		System.out.println("Value of m is " + m
						+ " and Value of n is " + n);
	}

	// Main driver method
	public static void main(String[] args)
	{
		// Random numbers of integer type
		int m = 9, n = 5;

		// Above function is called in main
		// to print swapped values of numbers
		swapValuesWithoutUsingThirdVariable(m, n);
	}
}

Output

Value of m is 5 and Value of n is 9

Approach 3: Swapping the Values Using Operator

Bitwise operators are used to operate on the individual bits of a number. They can be used with any integer type (char, short, int, etc.). They are used when performing update and query operations of the binary index tree. This operator is a binary operator, denoted by ‘^’. It returns the bitwise XOR of the input value, that is, if the corresponding bits are different, it returns 1; otherwise, it returns 0.

Illustration:

 
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7
  0101
^ 0111
 ________
  0010  = 2 (In decimal)

This is the most optimized method, because the direct calculation here is done by bits instead of bytes, as seen in the above two methods. This is a Java program that shows the inner workings-

 

// Java Program to swap the two values
// using XOR Operator

// Importing generic Java libraries
import java.io.*;

class GFG {

	// Function to swap values of two numbers
	// using XOR operator
	static void swapValuesUsingXOROperator(int m, int n)
	{
		// Logic of XOR operator
		m = m ^ n;
		n = m ^ n;
		m = m ^ n;

		System.out.println("Value of m is " + m
						+ " and Value of n is " + n);
	}

	// Main driver method
	public static void main(String[] args)
	{
		// Random two integer numbers
		// to get swapped
		int m = 9, n = 5;

		// Calling the function in main method
		// to get above integer numbers swapped
		swapValuesUsingXOROperator(m, n);
	}
}

Output

Value of m is 5 and Value of n is 9

 

Submit Your Programming Assignment Details