To swap two variables in Java without using a third variable, you can use arithmetic operations or bitwise XOR, and it has mathematical history rooted in algebra and logic gates. Here’s how you can do it:
Behind the Scenes: Why It's Interesting
Swapping variables is fundamental in:
- Sorting
algorithms (e.g., Bubble Sort, Selection Sort)
- Register-level
computing (assembly)
- Interview
puzzles
Doing it without a temporary variable shows an
understanding of memory, logic, and math.
Using Arithmetic Operations
java
public class
SwapWithoutThirdVariable { public static void main(String[] args) { int a = 30; int b = 40; System.out.println("Before Swap:
a = " + a + ", b = " + b); // Interchange values using arithmetic operations a = a + b; // The variable value a now reflects the total result of a and b b = a - b; //The variable value b now catch the original value of a a = a - b; // The variable value a now reflects the original value of b System.out.println("After Swap:
a = " + a + ", b = " + b); } } |
Using Bitwise XOR
java
public class
SwapWithoutThirdVariable { public static void main(String[] args) { int a = 30; int b = 40; System.out.println("Before Swap:
a = " + a + ", b = " + b); // Interchange values using bitwise XOR a = a ^ b; // The variable a now reflects the result of a XOR b b = a ^ b; // b currently catch the initial value of a a = a ^ b; // a currently catch the initial value of b System.out.println("After Swap:
a = " + a + ", b = " + b); } } |
Explanation
- Arithmetic
Operations:
- a =
a + b: Adds the values of a and b and stores the result in a.
- b =
a - b: Subtracts b from the new value of a (which is a + b), giving the
original value of a.
- a =
a - b: Subtracts the new value of b (which is the original value of a)
from a (which is a + b), giving the original value of b.
- Bitwise
XOR:
- a =
a ^ b: Computes the XOR of a and b and stores it in a.
- b =
a ^ b: XORs the new value of a (which is a ^ b) with b, yielding the
original value of a.
- a = a ^ b: XORs the new value of a (which is a ^ b) with the new value of b (which is the original value of a), yielding the original value of b.
Ex-plain:
A B A^B (A XOR B)
0 0 0 (zero
because operands are same)
0 1 1
1 0 1 (one
because operands are different)
1 1 0
Historical & Mathematical Roots
🔷 1. Algebraic Swap
(Addition-Subtraction)
- From
basic algebra: rearranging equations
- Invented
for low-level memory optimization, especially on machines with
limited registers
- Risk:
Might cause overflow if a + b exceeds the int range
🔷 2. Bitwise XOR Swap
- Comes
from Boolean algebra and logic gates:
- XOR
is reversible
- Originates
from digital circuit theory
- XOR
logic is used in CPU registers and low-level firmware
Comparison Table
Method |
Safe from Overflow |
Bitwise Knowledge |
Memory Used |
Speed |
History Root |
Add/Subtract |
❌ No |
✅ Basic |
✅ No temp |
⚡ Fast |
Algebra (Ancient) |
XOR |
✅ Yes |
✅ Required |
✅ No temp |
⚡ Fast |
Boolean Logic |
Temp variable |
✅ Yes |
❌ Not needed |
❌ Needs 1 |
✅ Fast |
High-level Logic |
Summary
Both techniques accomplish the exchange without the need for a temporary variable. The XOR method is more elegant and avoids potential issues with
integer overflow, but the arithmetic method is often easier to understand.
In short:
- Add/Subtract
method = algebraic manipulation from basic math
- XOR
method = inspired by logic gates and binary operations from computer
architecture
Swap of two data with out third variable |
For Custom information, visit:
Ø
Detect
& Handle Infinite Loops and Cycles in Linked Lists
Ø
Custom
Palindrome of a link list
Ø
Creating
a custom HashMap in Java
Ø
Custom
Combination and permutation program
Ø
Level
Order Traversal in Zig Zag pattern
For Security information, visit:
Ø
Asynchronous
Encryption and decryption without file only key pass
Ø
public
key encryption and private key decryption with keystore file
Ø
OWASP
(Open Web Application Security Project)
Ø
To
securely obtain employee information utilizing TLS 1.3 or TLS 1.2
Ø
Understanding
of Basic Auth, SAML, OAuth, Token, JWT Token, and SSL
0 Comments