In this tutorial we will learn how to swap two numbers in Java

swap two numbers in Java programming means swapping the values of two variables, which can be done using a temporary variable or without using a temporary variable. Swapping values is useful in programming, such as sorting or reordering elements in an array.

**Example:**

There are two variables m & n.

Value of x is β4β

Value of y is β5β.

Before Swapping: x value = 4; y value = 5

After Swapping: x value = 5; y value = 4

## How to Swap Two Numbers Using the third variable in JAVA

```
import java.util.Scanner;
import java.util.NoSuchElementException;
public class MyClass {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
try {
int m, n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
m = s.nextInt();
System.out.print("Enter the second number:");
n = s.nextInt();
temp = m;
m = n;
n = temp;
System.out.println("After Swapping");
System.out.println("First number:"+m);
System.out.println("Second number:"+n);
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above....");
}
}
}
```

### Output :

```
Enter the first number: 25
Enter the second number: 37
After Swapping
First number: 37
Second number: 25
```

### Complexities :

Time Complexity: O(1)

The time complexity of this program is O(1) since the number of operations performed is constant and does not depend on the size of the input.

Space Complexity: O(1)

The space complexity of this program is O(1) since only a constant amount of space is used to store the variables and the Scanner object.

## How to Swap two Numbers without using a third variable in JAVA

```
import java.util.Scanner;
public class Swap_Integers
{
public static void main(String args[])
{
int m, n;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
m = s.nextInt();
System.out.print("Enter the second number:");
n = s.nextInt();
m = m + n;
n = m - n;
m = m - n;
System.out.println("After Swapping");
System.out.println("First number:"+m);
System.out.println("Second number:"+n);
}
}
```

### Output :

```
Enter the first number: 4
Enter the second number: 5
After Swapping
First number: 5
Second number:4
```

### Complexities :

Time Complexity: O(1)

The time complexity of the above program is constant time or O(1), as the number of operations performed does not change with the size of the input.

Space Complexity: O(1)

The space complexity of the program is O(1), as it only uses a constant amount of memory to store the input variables and the Scanner object.

## How to Swap Two Numbers Using Bitwise Operator in JAVA

Swap two numbers using bitwise operator There are a number of methods to swap two numbers using Java. Today we will focus on swapping those numbers using a bitwise operator in Java. In computers, arithmetic calculations such as addition, division, subtraction, multiplication etc are done at a bit level. To perform these calculations, we have bitwise operators such as OR(|), AND(&), XOR(^), complement(~), shift right (>>), shift left(<<) etc. Bitwise is an XOR operator that compares bits of two numbers. If the numbers are equal, it returns the output as 1. If the numbers are not equal, it returns the output as 0.

For example, if we take the binary output of say number 1 which is: 00000101 and binary output of number 5 is: 00001010. As we compare the outputs of these 2 binary numbers, we can conclude that they are not the same on comparison. Hence, the output for the above numbers will be 0. The other methods for swapping two numbers are by using the temp variable, using an array, using the third variable with the arithmetic operator, using temp with multiplication and division etc. Swapping is a process in which the values of two integer numbers are exchanged with each other.

Below is a program to understand how we can swap two numbers using Java. For this, we will use the nextInt() method of the Scanner class.

### Swap two numbers using bitwise operator

```
import java.util.Scanner;
public class DeveloperHelps
{
public static void main(String args[])
{
int num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number:");
num1 = scanner.nextInt();
System.out.print("Enter second number:");
num2 = scanner.nextInt();
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
scanner.close();
System.out.println("First number after swapping is:"+num1);
System.out.println("Second number after swapping is:"+num2);
}
}
```

The output of the above program for swapping two number using bitwise operator in Java will be:

```
Enter first number: 160
Enter second number: 260
First number after swapping is: 260
Second number after swapping is: 160
```

Here we have used nextInt() method of the Scanner class to swap two numbers in Java. We use scanner class to break down the input into small token values. To know more about the scanner class, click here.