# Introduction

Swapping two numbers is a common programming task that involves exchanging the values of two variables. In Java, there are multiple approaches to achieving this swap operation. In this blog, we will explore a few simple and effective techniques to swapping of  two numbers in Java. We will also see some examples to help you understand the concept more easily.

Level up your skills in Java Programming. Enroll now and become an expert.

## Swapping of two numbers Using a Temporary Variable

One of the best methods to swap two numbers in Java is by using a temporary variable.

There are three stages of this approach –

1. Storing the value of one variable in a temporary
2. Assigning the value of the second variable to the first
3. Assigning the value of the temporary variable to the second

### Algorithm

Step 1: Initialize two variables, let’s say a and b, with the values you want to swap.

Step 2: Create a temporary variable, let’s call it temp, to hold the value temporarily.

Step 3: Assign the value of a to temp.

Step 4: Assign the value of b to a.

Step 5: Assign the value of temp to b.

Step 6: The values of a and b are now swapped.

Step 7: Continue with the rest of your program, using the swapped values if necessary.

Note: Ensure that the variable types of a, b, and temp are compatible with the numbers you want to swap.

Example:

O/P:

Before swapping: a = 10, b = 20

After swapping: a = 20, b = 10

#### Explore Technogeeks Best Programming Courses In Pune

 Python Programming Core Java Advanced Java SQL Programming JavaScript Programming C Programming

## Swapping of two numbers using Arithmetic Operations

Algorithm:

Step 1: Initialize two variables, let’s say x and y, with the values you want to swap.

Step 2:- Add the value of x to the value of y and store the result in x.

Step 3: Subtract the original value of a (before addition) from x and store the result in y.

Step 4: Subtract the original value of y (before addition) from y and store the result in x.

Step 5: The values of x and y are now swapped.

Example:

O/P:

Before swapping: x = 10, y = 20

After swapping: x = 20, y = 10

## Swapping of two numbers using XOR Operation

Algorithm:

Step 1: Initialize the two variables, let’s say a and b, with the values you want to swap.

Step 2: Perform the XOR operation between a and b and store the result in a.

Step 3: Perform the XOR operation again between the new value of a (after XOR) and b and store the result in b.

Step 4: Perform the XOR operation again between the new value of b (after the second XOR) and a and store the result in a.

Step 5: The values of a and b are now swapped

Output:

Before swapping: a = 10, b = 20

After swapping: a = 20, b = 10

## Swapping of two numbers using (swap) method

You can utilize the “Collections.swap()” method from the Java Collections framework, If you are working with collections in Java (specifically with ArrayLists).

This method simplifies the process by directly swapping the elements at specified indices.

Algorithm:

Step 1: Create an ArrayList or any other suitable collection.

Step 2: Add the numbers to the collection.

Step 3: Print the collection to display the numbers before swapping.

Step 4: Use Collections.swap() and provide the collection name along with the indices of the elements you want to swap.

Step 5: Print the collection again to display the numbers after swapping.

Example:

Output:

Before swapping: [10, 20]

After swapping: [20, 10]

## How To swap of two numbers without using third variable in java?

To swap two numbers in Java without using a third variable, you can use the arithmetic operations method & XOR Operation.

## Conclusion

Swapping of two numbers in java is a basic operation in programming, and Java offers various techniques to accomplish this task. Whether it’s using a temporary variable, arithmetic operations, XOR operations, or leveraging the Collections.swap() method, you now have a range of methods to choose from.

Understanding these swapping techniques equips you with the knowledge to manipulate variables effectively in your Java programs. Experiment with these examples, explore further, and enhance your programming skills with the ability to swap numbers effortlessly in Java.