# Java Program to Swap Two Numbers

Write a Java Program to Swap Two Numbers using a temporary variable, and also without using temporary or third variable. For this Java swap two numbers purpose, we are going to use Arithmetic Operators and Bitwise Operators.

## Java Program to Swap Two Numbers using Temp Variable

This program allows the user to enter two integer values. By using the third variable, this Java example Swaps those two numbers.

```// Java Program to Swap Two Numbers
import java.util.Scanner;

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

System.out.print(" Please Enter the First value : ");
a = sc.nextInt();

System.out.print(" Please Enter the Second value : ");
b = sc.nextInt();

System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);

temp = a;
a = b;
b = temp;

System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
}
}```

In the above Java example, we assigned a = 10 and b = 25

Temp = a – it means assigning a value to Temp variable
Temp = 10

a = b – assigning b value to the variable a
a = 25

b = Temp – assigning Temp value to b
b = 6

## Java Program to Swap Two Numbers without Using Temp Variable

In this Java example, Instead of using the temp or third variable to swap two numbers, we are going to use Arithmetic Operators.

```// Java Program to Swap Two Numbers
import java.util.Scanner;

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

System.out.print(" Please Enter the First value : ");
a = sc.nextInt();

System.out.print(" Please Enter the Second value : ");
b = sc.nextInt();

System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);

a = a + b;
b = a - b;
a = a - b;

System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
}
}```

Java swap two numbers using arithmetic operators

`````` Please Enter the First value : 6
Please Enter the Second value : 13

Before Swapping: a =  6 and b =  13

After Swapping:  a =  13 and b =  6``````

User Entered Values are a = 6 and b = 13

a = a + b
a = 6 + 13 = 19

b = a – b;
b = 19 – 13 = 6

a = a – b;
a = 19 – 6 = 13
Final Values after swapping 2 numbers: a = 13 and b = 6

NOTE: Although we can swap 2 numbers using multiplication and division approach. But it may produce strange values if we are working with larger integer values, or if any of the integer value is 0

## Java Program to Swap Two Numbers using Bitwise OR Operator

In this Java example, Instead of using the temp variable, we are going to use Bitwise Operators to swap 2 numbers.

```// Java Program to Swap Two Numbers
import java.util.Scanner;

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

System.out.print(" Please Enter the First value : ");
a = sc.nextInt();

System.out.print(" Please Enter the Second value : ");
b = sc.nextInt();

System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);

a = a ^ b;
b = a ^ b;
a = a ^ b;

System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
}
}```

Java swap two numbers using bitwise operators

`````` Please Enter the First value : 150
Please Enter the Second value : 275

Before Swapping: a =  150 and b =  275

After Swapping:  a =  275 and b =  150``````

## Java Program to Swap Two Numbers Using Method

This program to swap numbers is the same as the first example. But we separated the logic and placed it in a separate method.

```// Java Program to Swap Two Numbers
import java.util.Scanner;

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

System.out.print(" Please Enter the First value : ");
a = sc.nextInt();

System.out.print(" Please Enter the Second value : ");
b = sc.nextInt();

System.out.println("\n Before Swapping: a =  " + a + " and b =  " + b);
swapping(a, b);

}

public static void swapping(int a, int b)
{
int temp;

temp = a;
a = b;
b = temp;

System.out.println("\n After Swapping:  a =  " + a + " and b =  " + b);
}
}```
`````` Please Enter the First value : 12
Please Enter the Second value : 95

Before Swapping: a =  12 and b =  95

After Swapping:  a =  95 and b =  12``````