Java swap two integers – Java Program to Swap Two Numbers

Java swap two integers: Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Program to Swap Two Numbers

In this article we will see multiple ways to swap two numbers in Java.

When we swap two numbers, it means that values of two numbers are swapped. Generally two variables can be swapped by taking a third variable. There are also many more possible approaches like,

Now, let’s see the approaches one by one

Method-I :- Swap two values of numbers by using a third variable

With given two numbers we can swap the values by taking a third variable. Initially we assign the value of first variable in a third variable. Then we store the value of 2nd variable in the place of first variable. Then the third variable (which contains the value of 1st variable) is in assigned in 2nd variable.

Now the values are swapped i.e. 1st variable contain value of 2nd variable and 2nd variable contains the value of first variable.

Approach

  • Declare and assign two variable say a and b.
  • Let’s take a temporary (third) variable as temp.
  • Print the numbers before swapping.
  • Initially assign a’s value in temp.
  • Then assign b’s value in a. Now, b will have value of a.
  • Then assign temp’s value (initially contained a’s value) to b.
  • Now values are swapped.
  • Print the two numbers.

Program:

public class SwapTwoNumbers 
{

    public static void main(String[] args) 
{

        int a = 58, b = 72;
        //print the values of numbers before swapping
        System.out.println("--Before swapping--");
        System.out.println("First number = " + a);
        System.out.println("Second number = " + b);

        // Assign value of a in temporary(third) variable
        int temp = a;

        // Value of b will be stored in a 
        a = b;

        // Value of temp(temporary variable) initially contains a's value
        // a's value assigned to b
        b = temp;
        // print the values of number after swapping
        System.out.println("\n\n--After swapping--");
        System.out.println("First number = " + a);
        System.out.println("Second number = " + b);
    }
}

Output:

--Before swapping--
First number = 58
Second number = 72

--After swapping--
First number = 72
Second number = 58

Method-II :- Swap two numbers by taking inputs from user

In Java, java.util package provide a class i.e. Java Scanner class through which we can ask user to enter the inputs.

Then we can store the input of two variables and swap the values between them. Let’s try to implement this using below approach.

This method is similar to method-I but here we are asking user to give inputs.

Approach

  • Input two numbers from the user say a and b.
  • Let’s take a temporary (third) variable as temp.
  • Print the numbers before swapping.
  • Initially assign a’s value in temp.
  • Then assign b’s value in a. Now, b will have value of a.
  • Then assign temp’s value (initially contained a’s value) to b.
  • Now values are swapped.
  • Print the two numbers

Program:

import java.util.Scanner;
class SwapTwoNumbers
{
   public static void main(String args[])
   {
    int a,b,temp;
    // Asking user to give inputs
    System.out.println("Enter any two numbers : ");
    // create Scanner class objects to take the inputs
    Scanner sc = new Scanner(System.in);
      
    a = sc.nextInt();
    b = sc.nextInt();
    // print the values before swapping 
    System.out.println("--Before swapping--");
    System.out.println("First number = " + a);
    System.out.println("Second number = " + b);

    // Assign value of a in temporary(third) variable
    temp = a;
    // Value of b will be stored in a 
    a = b;
    // Value of temp (temporary variable) initially contains a's value
    // a's value assigned to b
    b = temp;
    
    // print the values after swapping
    System.out.println("--After swapping--");
    System.out.println("First number = " + a);
    System.out.println("Second number = " + b);
   }
}
Output:

Enter any two numbers :
5
8

--Before swapping--
First number = 5
Second number = 8

--After swapping--
First number = 8
Second number = 5

Method-III:- Swap two numbers without using a third variable

We can swap two numbers using arithmetic operators only. Let’s implement this using following approach.

Approach:

  • Declare and initialize two variables say ‘a’ and ‘b’.
  • Print the original values.
  • Initially store the sum of two numbers in a
  • Now for variable b assign the difference between a and b. Now indirectly b is assigned with original values of a.
  • Similarly to b assign the difference between a and b. Now indirectly a is assigned with original value of b.

Program:

class SwapTwoNumbers 
{
    public static void main(String args[])
    {
        // declare the numbers to be swapped
        int a = 29;
        int b = 65;
        //print the values of numbers before swapping
        System.out.println("--Before swapping--");
        System.out.println("First number = " + a);
        System.out.println("Second number = " + b);
    
        // Addition of both the values are stored in a
        a= a + b;
        // b value is assigned with the difference between a and b
        // i.e. now b contains a
        b = a - b;
        // difference of a and b i.e. total value-a is stored in a 
        a = a - b;
        
        // print the values of number after swapping
        System.out.println("\n\n--After swapping--");
        System.out.println("First number = " + a);
        System.out.println("Second number = " + b);
    }
}
Output:

--Before swapping--
First number = 29
Second number = 65

--After swapping--
First number = 65
Second number = 29

Method IV:- Swap values of two numbers using Bitwise XOR

We can also use Bitwise XOR operator to swap two variables. To implement this, we will use the below approach.

Approach

  • Declare and initialize two variables say ‘a’ and ‘b’.
  • Print the original values.
  • If we compute bitwise XOR of two numbers then it will give all bits as 1 wherever bits of the two number differs, otherwise it will give bits as 0.
  • Initially we will store a^b in a.
  • Then we will store a^b in b, i.e. now b will contain the original value of a.
  • Now we will again perform a^b and store this value in a, i.e. now a will contain original value of b.
  • Now two numbers are swapped.
  • Print the swapped numbers.

Program:

class SwapTwoNumbers 
{
    public static void main(String args[])
    {
        int a = 6;
        int b = 13;
        
        //print the values of numbers before swapping
        System.out.println("--Before swapping--");
        System.out.println("First number = " + a);
        System.out.println("Second number = " + b);

 
        // For XOR operations. x=0110, y=1101
        a = a ^ b; // a now becomes 11 (1011)
        b = a ^ b; // b becomes 6 (0110)
        a = a ^ b; // a becomes 13 (1101)
 
        // print the values of number after swapping
        System.out.println("\n\n--After swapping--");
        System.out.println("First number = " + a);
        System.out.println("Second number = " + b);

    }
}
Output:

--Before swapping--
First number = 6
Second number = 13

--After swapping--
First number = 13
Second number = 6

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Basic Programs: