Swapping of Two Numbers in Java
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.
Table of Content
- Swapping Two Numbers Using a Temporary Variable
- Swapping Two Numbers Using Arithmetic Operators + and โ
- Swapping Two Numbers Using Arithmetic Operators x and /
- Swapping Two Numbers Using Bitwise XOR operator ^
Best-suited Java courses for you
Learn Java with these high-rated online courses
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); }}
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 = 1
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); }}
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 = 1
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); }}
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 = 1
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); }}
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 = 1
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