Way 1: swap two numbers in Java: using a temporary variable.
To swap two numbers in Java, you can use a temporary variable to store the value of one number while you perform the swap. Here’s an example:
int a = 10;
int b = 20;
System.out.println("Before swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
int temp = a;
a = b;
b = temp;
System.out.println("After swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
Output:
Before swapping:
a = 10
b = 20
After swapping:
a = 20
b = 10
In the example above, we have two variables a
and b
representing the numbers to be swapped. The values of a
and b
are initially 10
and 20
, respectively. We use a temporary variable temp
to store the value of a
before swapping. Then we assign the value of b
to a
and finally assign the value of temp
(which holds the original value of a
) to b
. This way, the values of a
and b
are swapped.
Way 2 : swap two numbers in Java: using arithmetic operations.
you can swap two numbers in Java without using a temporary variable by using arithmetic operations. One way to do this is by using the addition and subtraction operations. Here’s an example:
public class NumberSwapExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swapping: a = " + a + ", b = " + b);
a = a + b; // Add both numbers and store the result in 'a'
b = a - b; // Subtract 'b' from the new value of 'a' and store the result in 'b'
a = a - b; // Subtract 'b' from the new value of 'a' and store the result in 'a'
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}
Output:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
In this approach, we use the addition operation to store the sum of both numbers in variable ‘a‘. Then, we use the subtraction operation to subtract the original value of ‘b‘ from the new value of ‘a‘ and store the result in ‘b‘. Finally, we subtract the new value of ‘b‘ from the new value of ‘a‘ and store the result in ‘a‘, effectively swapping the values of ‘a‘ and ‘b‘.
Way 3 : swap two numbers in Java: using the bitwise XOR (^) operator.
Another way to swap two numbers without using a temporary variable in Java is by using the bitwise XOR (^) operator. Here’s an example:
public class NumberSwapExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swapping: a = " + a + ", b = " + b);
a = a ^ b; // Perform bitwise XOR operation between 'a' and 'b' and store the result in 'a'
b = a ^ b; // Perform bitwise XOR operation between the new value of 'a' and 'b' and store the result in 'b'
a = a ^ b; // Perform bitwise XOR operation between the new value of 'a' and 'b' and store the result in 'a'
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}
Output:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
In this approach, we use the bitwise XOR operator (^) to perform the swapping. The XOR operation returns 1 if the corresponding bits of the operands are different and 0 if they are the same. By performing XOR operations between the numbers, we can effectively swap their values without using a temporary variable.
Way 4 : swap two numbers in Java: using multiplication and division operations.
another way to swap two numbers without using a temporary variable in Java is by using multiplication and division operations. Here’s an example:
public class NumberSwapExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swapping: a = " + a + ", b = " + b);
a = a * b; // Multiply 'a' and 'b' and store the result in 'a'
b = a / b; // Divide the new value of 'a' by 'b' and store the result in 'b'
a = a / b; // Divide the new value of 'a' by the new value of 'b' and store the result in 'a'
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}
Output:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
In this approach, we use multiplication and division operations to swap the values of ‘a‘ and ‘b‘. By multiplying ‘a‘ and ‘b‘ and storing the result in ‘a‘, we combine the values of both numbers. Then, by dividing the new value of ‘a‘ by ‘b‘, we obtain the original value of ‘a‘ in ‘b‘. Finally, by dividing the new value of ‘a‘ by the new value of ‘b‘, we obtain the original value of ‘b‘ in ‘a‘, effectively swapping the values of ‘a‘ and ‘b‘.
You can run above all code here just copy and paste.