|
| 1 | +--- |
| 2 | +title: Java Program to Swap Two Numbers |
| 3 | +shortTitle: Swap Two Numbers |
| 4 | +description: Learn different ways to swap two numbers in Java — using a temporary variable, arithmetic operations, and bitwise XOR — with clear examples and outputs. |
| 5 | +--- |
| 6 | + |
| 7 | +Swapping two numbers is a common beginner-friendly exercise in Java. Below are three simple ways to do it: |
| 8 | + |
| 9 | +- Using a temporary variable (safest and most readable) |
| 10 | +- Using arithmetic operations (beware of overflow for very large numbers) |
| 11 | +- Using bitwise XOR (no temp variable; works for integers) |
| 12 | + |
| 13 | +Before you start, you may want to review: |
| 14 | + |
| 15 | +- [Java Variables and Literals](/docs/variables-and-literals) |
| 16 | +- [Java Operators](/docs/operators) |
| 17 | +- [Java Basic Input and Output](/docs/basic-input-output) |
| 18 | + |
| 19 | +## 1) Swap Using a Temporary Variable |
| 20 | + |
| 21 | +This is the most straightforward and recommended approach. |
| 22 | + |
| 23 | +### Example |
| 24 | + |
| 25 | +```java |
| 26 | +class Main { |
| 27 | + public static void main(String[] args) { |
| 28 | + int a = 10; |
| 29 | + int b = 20; |
| 30 | + |
| 31 | + System.out.println("Before swap: a = " + a + ", b = " + b); |
| 32 | + |
| 33 | + int temp = a; // store a |
| 34 | + a = b; // put b into a |
| 35 | + b = temp; // put original a into b |
| 36 | + |
| 37 | + System.out.println("After swap: a = " + a + ", b = " + b); |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +#### Output |
| 43 | + |
| 44 | +```plaintext |
| 45 | +Before swap: a = 10, b = 20 |
| 46 | +After swap: a = 20, b = 10 |
| 47 | +``` |
| 48 | + |
| 49 | +## 2) Swap Without Temp (Arithmetic) |
| 50 | + |
| 51 | +Uses addition and subtraction. Note: This can overflow if the numbers are near the integer limits. |
| 52 | + |
| 53 | +### Example |
| 54 | + |
| 55 | +```java |
| 56 | +class Main { |
| 57 | + public static void main(String[] args) { |
| 58 | + int a = 15; |
| 59 | + int b = 27; |
| 60 | + |
| 61 | + System.out.println("Before swap: a = " + a + ", b = " + b); |
| 62 | + |
| 63 | + a = a + b; // a becomes sum |
| 64 | + b = a - b; // b becomes original a |
| 65 | + a = a - b; // a becomes original b |
| 66 | + |
| 67 | + System.out.println("After swap: a = " + a + ", b = " + b); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +#### Output |
| 73 | + |
| 74 | +```plaintext |
| 75 | +Before swap: a = 15, b = 27 |
| 76 | +After swap: a = 27, b = 15 |
| 77 | +``` |
| 78 | + |
| 79 | +## 3) Swap Using XOR (No Temp) |
| 80 | + |
| 81 | +Works for integers and avoids extra memory, but is less readable for beginners. |
| 82 | + |
| 83 | +### Example |
| 84 | + |
| 85 | +```java |
| 86 | +class Main { |
| 87 | + public static void main(String[] args) { |
| 88 | + int a = 5; |
| 89 | + int b = 9; |
| 90 | + |
| 91 | + System.out.println("Before swap: a = " + a + ", b = " + b); |
| 92 | + |
| 93 | + a = a ^ b; // Step 1 |
| 94 | + b = a ^ b; // Step 2: now b is original a |
| 95 | + a = a ^ b; // Step 3: now a is original b |
| 96 | + |
| 97 | + System.out.println("After swap: a = " + a + ", b = " + b); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +#### Output |
| 103 | + |
| 104 | +```plaintext |
| 105 | +Before swap: a = 5, b = 9 |
| 106 | +After swap: a = 9, b = 5 |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +### Which Method Should You Use? |
| 112 | + |
| 113 | +- Prefer the temporary variable method for clarity and safety. |
| 114 | +- The arithmetic method can be a neat trick, but avoid it when values may overflow. |
| 115 | +- XOR is an interesting technique; use it only when it adds real value and readability is not sacrificed. |
0 commit comments