Swap Two Numbers Without Using a Third Variable in Java Program

Introduction

Swapping two numbers is a fundamental programming task, often used in various algorithms and data manipulation processes. Typically, a third variable is used to temporarily hold one of the values during the swap. However, in this post, we’ll explore how to swap two numbers without using a third variable in Java.

Why Swap Without a Third Variable?

Swapping without a third variable can be an interesting problem-solving exercise that sharpens your understanding of arithmetic operations and bitwise operators. It can also be useful in scenarios with limited memory resources.

Methods to Swap Two Numbers

  1. Using Arithmetic Operations
  2. Using Bitwise XOR Operator

Let’s delve into both methods with Java examples.

Method 1: Using Arithmetic Operations

This method utilizes addition and subtraction to swap values.

Code Example

public class SwapNumbers {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        System.out.println("Before swap: a = " + a + ", b = " + b);

        // Swapping without a third variable
        a = a + b; // a becomes 30
        b = a - b; // b becomes 10
        a = a - b; // a becomes 20

        System.out.println("After swap: a = " + a + ", b = " + b);
    }
}

Explanation

  1. Addition: a = a + b;
    Adds both numbers and stores the result in a.
  2. Subtraction for b: b = a - b;
    Subtracts b from the new a to get the original value of a.
  3. Subtraction for a: a = a - b;
    Subtracts the new b (original a) from the new a to get the original value of b.

Method 2: Using Bitwise XOR Operator

This method uses the XOR bitwise operator for swapping.

Code Example

public class SwapNumbers {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        System.out.println("Before swap: a = " + a + ", b = " + b);

        // Swapping without a third variable using XOR
        a = a ^ b; // a becomes 30 (in binary: 1010 ^ 10100)
        b = a ^ b; // b becomes 10 (original value of a)
        a = a ^ b; // a becomes 20 (original value of b)

        System.out.println("After swap: a = " + a + ", b = " + b);
    }
}

Explanation

  1. XOR Operation: a = a ^ b;
    Performs XOR operation and stores the result in a.
  2. XOR for b: b = a ^ b;
    Performs XOR on the new a and b to get the original value of a.
  3. XOR for a: a = a ^ b;
    Performs XOR on the new a and the new b to get the original value of b.

Advantages of Each Method

  • Arithmetic Method: Simple and easy to understand.
  • Bitwise Method: More efficient for low-level programming and systems with limited arithmetic operation support.

Conclusion

Swapping two numbers without a third variable can be achieved efficiently using arithmetic operations or bitwise operators. This technique is a great exercise in understanding basic operations and can be handy in specific programming contexts.

Feel free to experiment with both methods and see which one fits your needs best. For more programming tutorials and code examples, stay tuned to our blog!

Additional Resources


Complete Code Snippet

Here is the complete Java code combining both methods:

public class SwapNumbers {
    public static void main(String[] args) {
        // Using Arithmetic Operations
        int a1 = 10, b1 = 20;
        System.out.println("Before swap (Arithmetic): a = " + a1 + ", b = " + b1);
        a1 = a1 + b1; // a1 becomes 30
        b1 = a1 - b1; // b1 becomes 10
        a1 = a1 - b1; // a1 becomes 20
        System.out.println("After swap (Arithmetic): a = " + a1 + ", b = " + b1);

        // Using Bitwise XOR Operator
        int a2 = 30, b2 = 40;
        System.out.println("Before swap (Bitwise): a = " + a2 + ", b = " + b2);
        a2 = a2 ^ b2; // a2 becomes 14 (in binary: 11110 ^ 101000)
        b2 = a2 ^ b2; // b2 becomes 30 (original value of a2)
        a2 = a2 ^ b2; // a2 becomes 40 (original value of b2)
        System.out.println("After swap (Bitwise): a = " + a2 + ", b = " + b2);
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top