Ways For Swapping Two Numbers In Java

Published on Aug 16,2019 6.5K Views

Ways For Swapping Two Numbers In Java

edureka.co

Swapping numbers can be crucial when dealing with data. In this article we will explore ways for Swapping two numbers In Java. Following pointers will be covered in this article,

Swapping two numbers in Java is something which every programmer must be aware of. There are mainly two methods to swap the numbers. These methods are discussed elaborately in this article.

Moving on with this article on Swapping two numbers in Java.

Swapping two numbers using a Temporary Variable

public class Main {
public static void main(String[] args) {
float a = 1.18f, b = 2.69f;
System.out.println("Before swapping");
System.out.println("First number = " + a);
System.out.println("Second number = " + b);
// Value of a is assigned to temporary
float temp = a;
// Value of b is assigned to first
a = b;
// Value of temp (which contains the initial value of first) is assigned to second
b = temp;
System.out.println("After swapping");
System.out.println("First number = " + a);
System.out.println("Second number = " + b);
}
}

Here, the numbers to be swapped are assigned to variables a and b. The first variable, i.e. a, is stored in variable temp, and the value of the second variable, i.e. b, is stored in the first variable.
The value of temp is then stored in b.

The output of the program is as follows:

Output:

Before swapping
First number = 1.18
Second number = 2.69
After swapping
First number = 2.69
Second number = 1.18

Moving on with this article on Swapping two numbers in Java.

Swapping two numbers without using a Temporary Variable

public class Main {
public static void main(String[] args) {
float a = 18.0f, b = 28.5f;
System.out.println("Before swapping:");
System.out.println("First number = " + a);
System.out.println("Second number = " + b);
a = a - b;
b = a + b;
a = b - a;
System.out.println("After swapping:");
System.out.println("First number = " + a);
System.out.println("Second number = " + b);
}
}

In the example, we have not made use of a temporary variable. Instead simple mathematics has been used:
a = a – b i.e. (18.0f – 28.5f)
The second number is then added to it:
b = a + b i.e. (18.0f – 28.5f) + 28.5f = 18.0f
To swap, the following logic is used:
a = b – a i.e. 18.0f – (18.0f – 28.5f) = 28.5f

The output of the program is as follows:

Output:

Before swapping:
First number = 18.0
Second number = 28.5
After swapping:
First number = 28.5
Second number = 18.0

Thus, the numbers can be swapped efficiently by using the methods discussed.

Thus we have come to an end of this article on ‘Swapping Two Numbers In Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES