Swapping numbers in Java

Swapping numbers in Java

Swapping basically means interchanging values of the variables. Usually swapping numbers becomes easy with the third variable. But it can be also done without third variable or using XOR (^) operation. Let’s see how to perform swapping of 2 numbers in java.

1. Using temporary/third variable-

import java.util.Scanner;
class swap {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter two numbers: ");
    int a = sc.nextInt();
    int b = sc.nextInt();
	int temp;
	temp=a;
	a=b;
	b=temp;
    System.out.println("Swapped numbers are " + a + " " + b);
  }
}

2. Without using temporary/third variable-

import java.util.Scanner;
class swap {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter two numbers: ");
    int a = sc.nextInt();
    int b = sc.nextInt();
	a=a+b;
	b=a-b;
	a=a-b;
    System.out.println("Swapped numbers are " + a + " " + b);
  }
}

3. Using Bitwise XOR-

import java.util.Scanner;
class swap {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter two numbers: ");
    int a = sc.nextInt();
    int b = sc.nextInt();
	a=a^b;
	b=a^b;
	a=a^b;
    System.out.println("Swapped numbers are " + a + " " + b);
  }
}

Output-

swap numbers java

Explanation-

Initially we declare three variables of integer type. Then we assign values to two variables. We display initial numbers given by user. The third variable swap stores the first number in it and second number is stored in a. Now the first number is stored in b.

For swapping numbers without third variable we use a different logic. The logic is first we add both numbers and store it in a. Then we subtract second number from first and store it in b. At last, we again subtract both the numbers and this time we store the answer in a. And that’s it, we have swapped our numbers.

In the third method we are not using any variable or addition, subtraction. instead we use bitwise XOR operation denoted by ^. The XOR of two numbers a and b returns a number which has all the bits as 1 wherever bits of x and y differ.

Program parts-

  1. class– Used to declare a class in java.
  2. public– Access modifier which represents it is visible to all.
  3. static– The main method is declared as static, so it doesn’t require to create an object to invoke the main method.
  4. void– Return type of the method.
  5. main– Represents the starting point of the program.
  6. Scanner– Scanner is a class in java. util package used for obtaining the input.
  7. String[] args– Used for command line argument.
  8. System.out.println()– System is a class, out is the object of PrintStream class, println() is the method of PrintStream class. System.out.println is used to print a statement.
  9. System.in is an InputStream.

Conclusion-

So this is how you do swapping of numbers in java. We hope it was clear to you. For daily updates and more such content do follow us on Instagram.

Keep programming!

ErrorFreeProgram.

2 COMMENTS

comments user
Yugant

Wow Excellent Post ๐Ÿ‘ Keep it up ๐Ÿ‘

    comments user
    ErrorFreeProgram

    Thank you so much. Your feedback means a lot.๐Ÿ˜€

Leave a Reply

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