Java Program to Swap Two Numbers

In this Java program, we will learn how to swap two numbers. Swapping means exchanging the values of two variables.

For example, if:

a = 10
b = 20

After swapping:

a = 20
b = 10

1. Normal Java Program Using a Temporary Variable

import java.util.Scanner;

public class SwapTwoNumbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = sc.nextInt();
        System.out.print("Enter second number: ");
        int b = sc.nextInt();
        System.out.println("Before Swapping:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        int temp = a;
        a = b;
        b = temp;
        System.out.println("After Swapping:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        sc.close();
    }
}

Output

Enter first number: 10
Enter second number: 20

Before Swapping:
a = 10
b = 20

After Swapping:
a = 20
b = 10

Explanation

A temporary variable is used to store the value of the first number:

int temp = a;

Then the value of b is assigned to a:

a = b;

Finally, the value stored in temp is assigned to b:

b = temp;

This exchanges the values of a and b.

Swapping Process

temp = a
a = b
b = temp

2. Java 8 Program to Swap Two Numbers

Java 8 does not provide a special swapping feature for primitive variables. The simplest approach is still to use a temporary variable.

We can, however, demonstrate the operation using a Java 8 lambda expression:

import java.util.Scanner;
import java.util.function.BiFunction;

public class SwapTwoNumbersJava8 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = sc.nextInt();

        System.out.print("Enter second number: ");
        int b = sc.nextInt();

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

        BiFunction<Integer, Integer, int[]> swap =
                (x, y) -> new int[]{y, x};

        int[] result = swap.apply(a, b);

        a = result[0];
        b = result[1];

        System.out.println("After Swapping:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);

        sc.close();
    }
}

Output

Enter first number: 10
Enter second number: 20

Before Swapping:
a = 10
b = 20

After Swapping:
a = 20
b = 10

Explanation of Java 8 Program

The Java 8 version uses the BiFunction functional interface:

BiFunction<Integer, Integer, int[]>

The lambda expression receives two numbers and returns them in reverse order:

(x, y) -> new int[]{y, x}

The apply() method executes the lambda expression:

int[] result = swap.apply(a, b);

The returned values are then assigned back to a and b.

Difference Between Normal Java and Java 8

FeatureNormal JavaJava 8
ApproachTemporary variableLambda + BiFunction
Easy to understandYesModerate
Extra objectNoint[]
Java 8 featureNoLambda expression
RecommendedYesFor learning Java 8

Conclusion

The temporary variable approach is the simplest and most recommended way to swap two numbers in Java. The Java 8 version demonstrates how a lambda expression and BiFunction can be used to perform the same operation.

For beginners, understanding the traditional approach first is important before moving to Java 8 functional programming.

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

Leave a Reply

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