Swapping of Two Numbers in Java

Swapping of Two Numbers in Java

3 mins read12.4K Views Comment
Atul
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.

2022_02_j.jpg

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.

Table of Content

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

โ€“ / โ€“
350 hours
Free
6 months
โ€“ / โ€“
4 months
โ€“ / โ€“
โ€“ / โ€“
โ€“ / โ€“
1 month
โ‚น50 K
7 weeks
โ‚น7.8 K
3 months
โ‚น8.47 K
2 months
โ‚น7 K
6 months
โ‚น4 K
2 months

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 = 2
After swapping: x = 2 y = 1
Copy 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 = 2
After swapping: x = 2 y = 1
Copy 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 = 2
After swapping: x = 2 y = 1
Copy 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 = 2
After swapping: x = 2 y = 1
Copy code
About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio