Swapping Two Variables in One Line in Java

Swapping two variables is a common task in programming, typically involving three steps: storing one variable's value in a temporary variable, assigning the second variable's value to the first, and then assigning the temporary variable's value to the second. However, in some programming languages and scenarios, it can be done more concisely in a single line. In Java, it can be achieved using arithmetic or bitwise operations. In this section, we will discuss how to swap two variables in one line.

Using Arithmetic Operations

The idea behind using arithmetic operations to swap two variables without a temporary variable is based on the properties of addition and subtraction.

File Name: SwapWithArithmetic.java

Output:

 
Before swap:
a = 5
b = 10
After swap:
a = 10
b = 5   

Explanation

The swap is done in a single line using the phrase a = a + b - (b = a). Initially, the numbers 5 and 10, respectively, are assigned to a and b. Three steps are involved in the operation of the expression: The sum of a and b is first calculated using a + b, yielding 15, which is then assigned to a. Concurrently, b is given the value of a prior to the addition (5) since (b = a). As a result, a now holds 15 and b holds 5. At the last stage, a = 15 - 5 gives a the initial value of b (10). Following the trade, b holds 5 and a holds 10.

Using Bitwise XOR Operation

Another method involves the bitwise XOR operation, which can also swap two integers without needing a temporary variable:

File Name: SwapWithXOR.java

Output:

 
Before swap:
a = 5
b = 10
After swap:
a = 10
b = 5   

Explanation

XOR is used to carry out the swap in the statement a = a ^ b ^ (b = a). At first, an is five and b is ten. The following is how the XOR operation works: Initially, a ^ b calculates the XOR of a and b, yielding 15, which is saved in a momentarily. At the same time, (b = a) gives b the initial value of a (5).

As a result, a now holds 15 and b holds 5. The original value of b (10) is assigned to an in the final step, which involves recalculating the XOR using a = 15 ^ 5. Following the trade, b holds five and a holds ten. This approach circumvents the overflow problems that arise during arithmetic operations and works well for integer types.

Time Complexity

The time complexity of above methods for swap operation are O(1) . The XOR operation and arithmetic operations, is a constant-time operation. The series of XOR operations and assignments each take a fixed amount of time, independent of the input values.

Space Complexity

The space complexity of above methods are O(1). Both approaches do not require any extra memory beyond the input variables a and b. The swap is performed in-place, and no additional variables or data structures are used.

Conclusion

In Java, bitwise XOR operations or arithmetic operations can be used to swap two variables in one line. These techniques offer a succinct means of carrying out the swap without using a temporary variable; but, they have certain drawbacks and require careful consideration of readability and type safety. As with any programming approach, maintaining manageable and comprehensible code requires striking a balance between cleverness and clarity.