Swapping of Two Numbers in Java

# Swapping of Two Numbers in Java

Atul Harsha
Senior Manager Content
Updated on Jan 7, 2023 22:05 IST

Get an introduction to swapping the values of two numbers in Java using different methods. Compare the use of a temporary variable, arithmetic operators, and bitwise operators for swapping numbers.

In Java, you can swap the values of two variables using a temporary variable, arithmetic operators, bitwise operators, or a combination of these techniques. In this blog we will explore different technique for swapping of two numbers in Java.

## Swapping Two Numbers in Java using a Temporary Variable

One common way to swap the values of two variables is to use a temporary variable to store one of the values, and then assign the other value to the first variable and the temporary value to the second variable. For example:

` `
```public class Main { public static void main(String[] args) { int x = 1; int y = 2; System.out.println("Before swapping: x = " + x + " y = " + y); // Use a temporary variable to store the value of x int temp = x; // Assign the value of y to x x = y; // Assign the value of the temporary variable (the original value of x) to y y = temp; System.out.println("After swapping: x = " + x + " y = " + y); }}Copy code```

### Explanation:

• Initialize the variables x and y with the values 1 and 2, respectively.
• Use a temporary variable to store the value of x.
• Assign the value of y to x.
• Assign the value of the temporary variable (the original value of x) to y.
• Print the values of x and y before and after the swap.

### Output:

` `
`Before swapping: x = 1 y = 2After swapping: x = 2 y = 1Copy code`

## Swapping Two Numbers Using Arithmetic Operators + and –

` `
```public class Main { public static void main(String[] args) { int x = 1; int y = 2; System.out.println("Before swapping: x = " + x + " y = " + y); // Swap the numbers using addition and subtraction x = x + y; // x is now 3 y = x - y; // y is now 1 x = x - y; // x is now 2 System.out.println("After swapping: x = " + x + " y = " + y); }}Copy code```

### Explanation:

• Initialize the variables x and y with the values 1 and 2, respectively.
• Use the following sequence of arithmetic operations to swap the values of x and y:
• Add y to x and assign the result to x. This stores the sum of x and y in x.
• Subtract y from the new value of x and assign the result to y. This stores the original value of x in y.
• Subtract y from the new value of x and assign the result to x. This stores the original value of y in x.
• Print the values of x and y before and after the swap.

### Output:

` `
`Before swapping: x = 1 y = 2After swapping: x = 2 y = 1Copy code`

## Swapping Two Numbers Using Arithmetic Operators x and /

` `
```public class Main { public static void main(String[] args) { int x = 1; int y = 2; System.out.println("Before swapping: x = " + x + " y = " + y); // Swap the numbers using division and multiplication x = x * y; // x is now 2 y = x / y; // y is now 1 x = x / y; // x is now 2 System.out.println("After swapping: x = " + x + " y = " + y); }}Copy code```

### Explanation:

• Initialize the variables x and y with the values 1 and 2, respectively.
• Use the following sequence of arithmetic operations to swap the values of x and y:
• Multiply x by y and assign the result to x. This stores the product of x and y in x.
• Divide the new value of x by y and assign the result to y. This stores the original value of x in y.
• Divide the new value of x by the new value of y and assign the result to x. This stores the original value of y in x.
• Print the values of x and y before and after the swap.

### Output:

` `
`Before swapping: x = 1 y = 2After swapping: x = 2 y = 1Copy code`

## Swapping Two Numbers Using Bitwise XOR operator ^

` `
```public class Main { public static void main(String[] args) { int x = 1; int y = 2; System.out.println("Before swapping: x = " + x + " y = " + y); // Swap the numbers using bitwise XOR x = x ^ y; // x is now 3 (011 ^ 010 = 011) y = x ^ y; // y is now 1 (011 ^ 010 = 001) x = x ^ y; // x is now 2 (011 ^ 001 = 010) System.out.println("After swapping: x = " + x + " y = " + y); }}Copy code```

### Explanation:

• Initialize the variables x and y with the values 1 and 2, respectively.
• Use the following sequence of bitwise XOR operations to swap the values of x and y:XOR x with y and assign the result to x. This stores the XOR of x and y in x.
• XOR the new value of x with y and assign the result to y. This stores the original value of x in y.
• XOR the new value of x with the new value of y and assign the result to x. This stores the original value of y in x.
• Print the values of x and y before and after the swap.

### Output:

` `
`Before swapping: x = 1 y = 2After swapping: x = 2 y = 1Copy code`