Java Program to Swap two numbers

by | Aug 7, 2021 | Java Programs

Home » Java » Java Programs » Java Program to Swap two numbers

Introduction

This article deals with how two numbers in Java can be swapped. The problem statement in this case is that there are two integers (the datatype of the numbers being irrelevant here) m and n. Our objective is to swap the values of the two integers such that m acquires the previous value of n and n acquires the previous value of m. The subsequent java code to perform this operation is illustrated below.

Illustration

Input : m = 9, n = 5
Output: m = 5, n = 9

Input : m = 15, n = 5
Output: m = 5, n = 15

For the sake of this example, we will be considering m and n both to be of integer data type.

Methods of Swapping

There are three fundamental ways of swapping two numbers in Java varying from space ad time complexity.

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

Using a Third Variable to Swap Values

This process is one of the most popular methods used for swapping the values of the two variables. In this process, a third variable of the same datatype of that of the two numbers to be swapped is created. This is a local variable and normally referred to as a temporary variable. While swapping, the variable of the first number is stored in this temporary variable. Thereafter the value of the second variable is assigned to the first variable. The value of the temporary variable is final attached to the second variable.

import java.util.*;
class Demo1
{
static void swapUsingaTempVariable (int m, int n)
{
int temp = m;
m = n;
n = temp;
System.out.println (“Value of m is ” + m + “and Value of n is ” + n);
}
public static void main (String args [])
{
int m = 9, n = 5;
swapUsingTempVariable (m,n)
}
}

 

OUTPUT

Value of m is 5 and value of  is 9

Swapping the Two numbers without using a Third Variable

  • The difference of the first number from the second number is calculated and assigned to the first variable.
  • The sum of the two numbers in calculated having done the step mentioned earlier. This value is stored in the second variable.
  • Finally, the first number is subtracted from the second number considering both the steps mentioned before this has been performed. This value is then assigned to the first variable.

Sample Program

import java.util.*;
class Demo2
static void swapWithoutTemp( int m, int n)
{
m = m – n;
n = m + n;
m = n – m;
System.out.println (“Value of m is ” + m + “and value of n is ” + n);
}
public static void main (String args [])
{
int m = 9, n = 5;
swapWithoutTemp (m , n);
}
}

 

OUTPUT

Value of m is 5 and Value of n is 9

Swapping Values Using an Operator

The main purpose of a bit-wise operator is to undergo manipulations of individual bits of a number. They can be used with any of the integral types (char, short, int, etc). When considering operations like Binary indexed trees and the like, this comes in very handy. The exclusive OR operator is denoted by ‘^’. If the corresponding bits are different, the output is 1. For all other cases, the output is 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 considered to be the most efficient method as here the operation on the bits is carried out directly rather than on bytes as in the case of the previous two methods.

import java.io.*;
class Demo3
static void swapXOR (int m , int n)
{
m = m ^ n;
n = m ^ n;
m = m ^ n;
System.out.println (“Value of m is  ” + m + “and Value of n is  ” + n);
}
public static void main (String args [])
{
int m = 9; n = 5;
swapXOR (m , n);
}
}

 

OUTPUT

Value of m is 5 and Value of n is 9

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author