# Java Program to Swap Two Numbers

In this post, you will learn to swap two numbers in Java Programming language using different methods.

Writing a Number Swapping Program in Java Programming language is good for logic building. You get to know different aspects of logic building and how to implement them in your program to get the desired output.

Now, let’s see the Java program to swap two numbers.

## Java Program to Swap Two Numbers

### Method 1 : using third variable

```import java.util.*;

public class Main{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
int firstNum = sc.nextInt();
System.out.println("Enter second number: ");
int secondNum = sc.nextInt();

//Before Swapping two numbers
System.out.println("Before Swapping :");
System.out.println("First number is "+firstNum);
System.out.println("Second number is "+secondNum);

//Swapping the numbers
int temp = firstNum;
firstNum = secondNum;
secondNum = temp;

//After Swapping two numbers
System.out.println("After Swapping :");
System.out.println("First number is "+firstNum);
System.out.println("Second number is "+secondNum);

}

}```

Output

``````Enter First Number: 123
Enter Second Number: 456

Before Swapping :
First number is 123
Second number is 456

After Swapping :
First number is 456
Second number is 123
``````

## How Does This Program Work

``````    // Swapping the numbers

int temp = firstNum; // Value of firstNum is assigned to temp
firstNum = secondNum; // Value of secondNum assigned to firstNum
secondNum = temp; // Value of temp assigned to secondNum
``````

In the above code, the temp variable is assigned the value of firstNum variable. Then, the value of secondNum variable is assigned to the firstNum variable. And finally, the temp variable( which holds the value of firstNum variable) is assigned to secondNum variable

For example,
firstNum = 10 and secondNum = 20
then we do, temp = firstNum i.e. temp = 10
then, firstNum = secondNum i.e. firstNum = 20 (now)
then, secondNum = temp i.e. secondNum = 10
Here, we don’t assign the value of firstNum to secondNum because the value of firstNum has been changed to that of secondNum that’s why we need a temp variable to store the value of firstNum variable so that the value doesn’t get lost.

This completes our swapping logic of Method 1.

Now, let’s see the Java Program to swap two numbers without using any 3rd variable (temp)

### Method 2 : without using any third variable

```import java.util.*;

public class Main{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
int firstNum = sc.nextInt();
System.out.println("Enter second number: ");
int secondNum = sc.nextInt();

//Before Swapping two numbers
System.out.println("Before Swapping :");
System.out.println("First number is "+firstNum);
System.out.println("Second number is "+secondNum);

//Swapping the numbers without using third variable
firstNum = firstNum + secondNum;
secondNum = firstNum - secondNum;
firstNum = firstNum - secondNum;

//After Swapping two numbers
System.out.println("After Swapping :");
System.out.println("First number is "+firstNum);
System.out.println("Second number is "+secondNum);

}

}```

Output

``````Enter First Number: 123
Enter Second Number: 456

Before Swapping :
First number is 123
Second number is 456

After Swapping :
First number is 456
Second number is 123
``````

## How Does This Program Work

``````//Swapping the numbers without using third variable
firstNum = firstNum + secondNum;
secondNum = firstNum - secondNum;
firstNum = firstNum - secondNum;``````

In the above logic, we have used a basic arithmetic calculation to swap the numbers.
Let’s see that using an example will make it easy to understand the logic.

Suppose, a = 10 and b = 20
a = a + b = 10 + 20 = 30
b = a – b = 30 – 20 = 10
a = a – b = 30 – 10 = 20
Hence, the values are swapped with each other.

## Conclusion

I hope after going through this post, you understand how to code Java Program to Swap Two Numbers using 2 different methods.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.