Java Program to Swap Two Numbers

Java Program to Swap Two Numbers

In this program, you'll learn two techniques to swap two numbers in Java. The first one uses a temporary variable for swapping, while the second one doesn't use any temporary variables.

Example 1: Swap two numbers using temporary variable

public class SwapNumbers {

   public static void main(String[] args) {

        float first = 2.40f, second = 2.21f;

        System.out.println("--Before swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);

        // Value of first is assigned to temporary
        float temporary = first;

        // Value of second is assigned to first
        first = second;

        // Value of temporary (which contains the initial value of first) is assigned to second
        second = temporary;

        System.out.println("--After swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);
    }
}

 

When you run the program, the output will be:

--Before swap--

First number = 2.4

Second number = 2.21

--After swap--

First number = 2.21

Second number = 2.4

In the above program, two numbers 2.40f and 2.21f which are to be swapped are stored in variables: first and second respectively.

The variables are printed before swapping using println() to see the results clearly after swapping is done.    

  • First, the value of first is stored in variable temporary (temporary = 2.40f).    

  • Then, value of second is stored in first (first = 2.21f).    

  • And, finally value of temporary is stored in second (second = 2.40f)


  •  

This completes the swapping process and the variables are printed on the screen.

Remember, the only use of temporary is to hold the value of first before swapping. You can also swap the numbers without using temporary.


 

Example 2: Swap two numbers without using temporary variable

public class SwapNumbers {

    public static void main(String[] args) {

        float first = 15.0f, second = 25.0f;

        System.out.println("--Before swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);

        first = first - second;
        second = first + second;
        first = second - first;

        System.out.println("--After swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);
    }
}

 

 output :

--Before swap--

First number = 15.0

Second number = 25.0

--After swap--

First number = 25.0

Second number = 15.0

In the above program, instead of using temporary variable, we use simple mathematics to swap the numbers.

For the operation, storing (first - second) is important. This is stored in variable first.

first = first - second; first = 15.0f - 25.0f

Then, we just add second (25.0f) to this number - calculated first (15.0f - 25.0f) to swap the number.

second = first + second; second = (15.0f - 25.0f) + 25.0f = 15.0f

Now, second holds 15.0f (which was initially value of first). So, we subtract calculated first (15.0f - 25.0f) from swapped second (15.0f) to get the other swapped number.

first = second - first;

first = 15.0f - (15.0f - 25.0f) = 25.0f

The swapped numbers are printed on the screen using println().


Subscribe For Daily Updates

JAVA File IO examples

JAVA Date & Time Progamming Examples

JAVA Collections

Flutter Questions
Android Questions