Java Program – Swap Two Numbers

In this tutorial, we shall write Java Program to swap two numbers. The numbers could be integers, float, double, long, etc.

In our first example, we shall write a Java program that uses a temporary variable to swap two numbers.

In our second example, we shall write a Java program that swaps two numbers in place. No third variable is used. We shall use the existing two variables holding the two numbers.

Example 1 – Swap Two Numbers using Temporary Variable

In this example, we take two numbers in two variables. Then we shall use a temporary variable to hold one of the numbers while we are trying to swap the values between the two variables.

Algorithm

Following is the algorithm we shall use to swap the given two numbers using a third variable.

  1. Start.
  2. Read a number in num1.
  3. Read a number in num2.
  4. Declare a variable temp.
  5. Assign temp with num1.
  6. Assign num1 with num2.
  7. Assign num2 with temp.
  8. Print num1 and num2.
  9. Stop.

Example.java

/**
 * Java Program - Swap Two Integers
 */

public class Example {

	public static void main(String[] args) {
		//two numbers
		int num1 = 12;
		int num2 = 87;
		
		//temporary variable
		int temp;
		
		//swap two numbers
		temp = num1;
		num1 = num2;
		num2 = temp;
		
		//print the numbers
		System.out.println("num1 : "+num1);
		System.out.println("num2 : "+num2);
	}
}

Explanation

int num1 = 12; declares num1 as integer and initializes num1 with 12.

int num2 = 87; declares num2 as integer and initializes num2 with 87.

int temp; declares temp as integer. We shall use this as temporary variable to hold one of the values of the two numbers.

temp = num1; assigns value in num1 to temp variable. So, we are saving the value of of num1 to temp. Now we can do anything to num1, because we have its value stored in other variable temp. After this step, the variables have the values as shown in the following.

num1 = 12;
num2 = 87;
temp = 12;

num1 = num2; assigns the value of num2 to num1. So, num1 will have a value of 87 stored in it.

num1 = 87
num2 = 87
temp = 12

num2 = temp; assigns the value of temp to num2. So, num2 will have a value of 12 stored in it.

num1 = 87
num2 = 12
temp = 12

We started with num1 and num2 having values of 12 and 87 respectively. After swapping, we are left with num1 and num2 having values 87 and 12 respectively.

We swapped the values in the variables.

Run the above program, and you shall get the following output.

num1 : 87
num2 : 12
ADVERTISEMENT

Example 2 – Swap Two Numbers – In place

In this example, we take two numbers in two variables. We shall not use another temporary variable, but just these two variables to swap the numbers. Let us see how.

Example.java

/**
 * Java Program - Swap Two Integers
 */

public class Example {

	public static void main(String[] args) {
		//two numbers
		int num1 = 12;
		int num2 = 87;
		
		//swap numbers
		num1 = num2+num1;
		num2 = num1-num2;
		num1 = num1-num2;
		
		//print the numbers
		System.out.println("num1 : "+num1);
		System.out.println("num2 : "+num2);
	}
}

Explanation

int num1 = 12; declares num1 as integer and initializes num1 with 12.

int num2 = 87; declares num2 as integer and initializes num2 with 87.

num1 = num2+num1; computes the sum of both num1 and num2, and stores the result in num1. After the execution of this statement, values of num1 and num2 would be as shown below.

num1 = 99
num2 = 12

num2 = num1-num2; computes the difference of num1 and num2, and stores the result in num2. After the execution of this statement, values of num1 and num2 would be as shown below.

num1 = 99
num2 = 87

Please note that num2 contains the value of initial num1.

num1 = num1-num2; computes the difference of num1 and num2, and stores the result in num1. After the execution of this statement, values of num1 and num2 would be as shown below.

num1 = 12
num2 = 87

The values in the two variables are swapped.

Runt the above Java program. You shall get the output in console, as shown below.

num1 : 87
num2 : 12

Conclusion

In this Java Tutorial, we learned how to swap two integers, in different ways. The process is same for float, int, or double. Just the datatype of num1 and num2 changes.