English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collections

Java Set Collections

Java Input Output (I/O)/O)

Java Reader/Writer

Java Other Topics

Java program to swap two numbers

Java Comprehensive Examples

In this program, you will learn two techniques to swap two numbers in Java. The first one uses a temporary variable for the swap, while the second one does not use any temporary variable.

示例1:To swap two numbers using a temporary variable

public class SwapNumbers {
    public static void main(String[] args) {
        float first = 1.20f, second = 2.45f;
        System.out.println("--Before swapping--");
        System.out.println("The first number = ", + first);
        System.out.println("The second number = ") + second);
        //The value of first is assigned to temporary
        float temporary = first;
        //The value of second is assigned to first
        first = second;
        //The temporary value (including the initial value of first) is assigned to second
        second = temporary;
        System.out.println("--After swapping--");
        System.out.println("The first number = ", + first);
        System.out.println("The second number = ") + second);
    }
}

When the program is run, the output is:

--Before swapping--
The first number = 1.2
The second number = 2.45
--After swapping--
The first number = 2.45
The second number = 1.2

In the above program, the two numbers to be swapped are1.20f and2.45f are stored in the variables first and second.

Use println() to print the variables before the swap to clearly see the results after the swap is completed.

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

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

  • And, the final value of temporary is stored in second (second = 1.2in the value 0f).

Thus, the swap process is completed, and the variables are printed on the screen.

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

示例2:To swap two numbers without using a temporary variable

public class SwapNumbers {
    public static void main(String[] args) {
        float first = 12.0f, second = 24.5f;
        System.out.println("--Before swapping--");
        System.out.println("The first number = ", + first);
        System.out.println("The second number = ") + second);
        first = first - second;
        second = first + second;
        first = second - first;
        System.out.println("--After swapping--");
        System.out.println("The first number = ", + first);
        System.out.println("The second number = ") + second);
    }
}

When the program is run, the output is:

--Before swapping--
The first number = 12.0
The second number = 24.5
--After swapping--
The first number = 24.5
The second number = 12.0

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

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

first = first - second;
first = 12.0f - 24.5f

Then, we just addplus second(24.5f)-The calculated first(12.0f - 24.5f) to swap the number.

second = first + second;
second = (12.0f - 24.5f) + 24.5f = 12.0f

Now, second holds12.0f (which was originally the value of first). Therefore, we take the swapped second(12.0f) by subtracting the calculated first(12.0f - 24.5f) gets another swapped number.

first = second - first;
first = 12.0f - (12.0f - 24.5f) = 24.5f

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

Java Comprehensive Examples