Java Program to Swap two number using third Variable

In this tutorial you will learn swapping of two numbers using third variable Java in  programming language.

This is also a most important program which commonly asked in interview.

Before directly moving on writing the program lets understand what is our aim to achieve in this program.

Basically Swapping of number means 

Suppose variable a has assigned value is 2 and the variable b has assigned value 4 then in swapping operation we will exchange the value and assign value of a i.e 2 in b and the value of b i.e. 4 in variable a.

How our program will behave?

As we already seen above that what we have to achieve in the program.

In the swapping program without using third variable we will assign two different value to the different variables.

For example: a=2 and b=4

Now after execution of the program our output should like 

a=4 and b = 2

Program 1: Swap two numbers using third variable in Java

import java.util.*;  
class Main{
    public static void main(String ...args){
        int tempvar;
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter first number- ");  
        int firstno= sc.nextInt();  
        System.out.print("Enter second number- ");  
        int secondno= sc.nextInt();  
        System.out.println("Before swapping number are : "+firstno+" and "+secondno);  
        tempvar=firstno;
	    firstno=secondno;
	    secondno=tempvar;
	    System.out.println("After swapping number are : "+firstno+" and "+secondno);
    }
} 

Output:

Enter first number- 56
Enter second number- 34
Before swapping number are : 56 and 34
After swapping number are : 34 and 56

Program 2: Swap two numbers using Java 8

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 10);

        System.out.println("Before swapping number are : " + numbers);

        numbers = numbers.stream()
                .collect(Collectors.collectingAndThen(Collectors.toList(), list -> {
                    Collections.swap(list, 0, 1);
                    return list;
                }));

        System.out.println("After swapping number are : " + numbers);
    }
}

Output

Before swapping number are : [5, 10]
After swapping number are : [10, 5]

Conclusion

In this tutorial we have seen two different approaches for swapping two numbers in Java. In the first approach we have used traditional method using a third variable to perform the swap in Java. This method is straightforward, widely understood, and often a common question in interviews.

In the second program we have taken the advantage of Java 8 Stream API to swap the numbers. While the Stream API is powerful for functional-style data processing, it is not the most intuitive choice for a simple variable swap. This approach utilized the collectingAndThen collector along with Collections.swap to achieve the desired result.

Both programs successfully swapped the values of two variables and displayed the results before and after the swap. The choice between the two methods depends on factors such as readability, simplicity, and the specific requirements of the task at hand. The first program using a third variable is a classic and widely accepted approach, while the second program with Java 8 features demonstrates a more unconventional but interesting use of modern Java features.

Happy Coding!!

Leave a Comment