Java Swap

Hello everyone!! Welcome to flower brackets blog. Today we will discuss java swap.

java swap

In this post let’s see how to swap variables in java. Well, there are two ways to swap numbers in java,

Also Read – Add Two Numbers With Bitwise Operator

  1. with temporary variable
  2. without temporary variable

Example : java swap

/* swapping variables in java WITH TEMPORARY VARIABLE */

import java.util.Scanner;

public class SwapTwoNumbers
{
   public static void main(String[] args)
   {
      int a;
      int b;
      int swap;
 
      System.out.println("Please enter two integers to swap : ");
      Scanner sc = new Scanner(System.in);
      a = sc.nextInt();
      b = sc.nextInt();
 
      System.out.println("Before swap : a = " + a + "\nBefore swap : b = " + b);
      swap = a;
      a = b;
      b = swap;
      System.out.println("After swap : a = " + a + "\nAfter swap : b = " + b);
   }
}

Output :

java swap


/* swapping variables in java WITHOUT TEMPORARY VARIABLE */

import java.util.Scanner;

public class SwapTwoNumbers
{
   public static void main(String[] args)
   {
      int a;
      int b; 
 
      System.out.println("Please enter two integers to swap : ");
      Scanner sc = new Scanner(System.in);
      a = sc.nextInt();
      b = sc.nextInt();
 
      System.out.println("Before swap : a = " + a + "\nBefore swap : b = " + b);
      a = a + b;
      b = a - b;
      a = a - b;
      System.out.println("After swap : a = " + a + "\nAfter swap : b = " + b);
   }
}

Output :

Please enter two integers to swap : 10 5
Before swap : a = 10
Before swap : b = 5
After swap : a = 5
After swap : b = 10


Swapping variables in java using method

Now let’s learn how to swap variables in java using method. Here’s the code,

public class SwapNumbers 
{
   public static void main(String[] args) 
   { 
      int num1 = 10;
      int num2 = 20;

      System.out.println("Before swapping : ");
      System.out.println("Value of num1 is : " + num1);
      System.out.println("Value of num1 is : " + num2);

      swap(num1,num2);
   }

   private static void swap(int num1, int num2)
   {
      int temp = num1;
      num1 = num2;
      num2 = temp;

      System.out.println("After swapping : ");
      System.out.println("Value of num1 is : " + num1);
      System.out.println("Value of num1 is : " + num2);
   }
}

Output :

Before swapping :
Value of num1 is : 10
Value of num1 is : 20
After swapping :
Value of num1 is : 20
Value of num1 is : 10


Another way to swap in java

Here in this example we are going to take input from the user to swap two numbers in java. Let’s see the code,

import java.util.Scanner;

public class SwapJava 
{
   public static void main(String[] args) 
   {
      int a, b, c;
      Scanner sc = new Scanner(System.in);

      System.out.println("Please enter the first number : ");
      a = sc.nextInt();
      System.out.println("Please enter the second number : ");
      b = sc.nextInt();

      c = a;
      a = b;
      b = c;

      System.out.println("Swap two numbers in java : " + a + " and " + b);
      sc.close();
   }
}

Output :

Please enter the first number : 50
Please enter the second number : 100
Swap two numbers in java : 100 and 50


Using bitwise operator

Here we are going to learn swap numbers without temp. So to swap two numbers in java we will use bitwise operator.

In bitwise operator, bitwise operator returns 0 if both operand is equal and returns 1 if both operands are not equal.

public class SwapVariables 
{
   public static void main(String[] args) 
   {
      int a = 2;
      int b = 4;

      System.out.println("Before : Swap numbers without temp, a : " + a + " b : " + b);

      // using XOR bitwise operator 
      a = a ^ b;
      b = a ^ b;
      a = a ^ b;

      System.out.println("Swapping variables without temp using XOR bitwise operation, a : " + a + " b : " + b);
   }
}

Output :

Before : Swap numbers without temp, a : 2 b : 4
Swapping variables without temp using XOR bitwise operation, a : 4 b : 2


conclusion

That’s it guys. This was all about how to swap variables in java. I hope you have understood the concept of swapping variables without temp using XOR bitwise operation.

Do share this article if you like.

You May Also Like