Step-by-Step Guide to Swap Two Numbers in Java :

Step-by-Step Guide to Swap Two Numbers in Java :

Swapping Two Numbers in Java

Problem Description

We are given two variables, a and b, with initial values. The task is to swap the values of these two variables.

Examples

  1. Example 1:

    • Input: a = 10, b = 20

    • Output: a = 20, b = 10

  2. Example 2:

    • Input: a = "Geeks", b = "gfg"

    • Output: a = "gfg", b = "Geeks"

Incorrect Approach

javaCopy codeint a = 10;
int b = 20;

a = b;
b = a;
  • This approach fails because:

    • When a = b is executed, a becomes 20.

    • When b = a is executed, b also becomes 20.

    • Both a and b end up being 20, which is incorrect.

Correct Approach

To swap two values correctly, we need an extra temporary variable.

Steps:
  1. Initialization:

     javaCopy codeint a = 10;
     int b = 20;
     int temp = 0;  // temporary variable
    
  2. Swapping Process:

    1. Store the value of a in temp:

       javaCopy codetemp = a;  // temp = 10
      
    2. Assign the value of b to a:

       javaCopy codea = b;  // a = 20
      
    3. Assign the value stored in temp to b:

       javaCopy codeb = temp;  // b = 10
      
  3. Output:

     javaCopy codeSystem.out.println("a = " + a);  // a = 20
     System.out.println("b = " + b);  // b = 10
    

Java Implementation

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

        // Temporary variable for swapping
        int temp = a;
        a = b;
        b = temp;

        // Print the swapped values
        System.out.println("a = " + a);  // a = 20
        System.out.println("b = " + b);  // b = 10
    }
}

Explanation with Memory Blocks

  1. Initial Values:

    • a points to 10.

    • b points to 20.

  2. After temp = a:

    • temp points to 10 (value of a).
  3. After a = b:

    • a points to 20 (value of b).
  4. After b = temp:

    • b points to 10 (value stored in temp).

Interview Questions

  1. Why can't we directly assign a = b and b = a to swap values?

    • Direct assignment loses the original value of a, making it impossible to correctly assign b.
  2. Can you swap values without using a temporary variable?

    • Yes, by using arithmetic or bitwise operations.
  3. How would you swap two strings in Java?

    • The process is the same as with numbers:

        javaCopy codeString a = "Geeks";
        String b = "gfg";
        String temp = a;
        a = b;
        b = temp;
      
  4. What are other methods to swap two variables in Java?

    • Using arithmetic operations:

        javaCopy codea = a + b;
        b = a - b;
        a = a - b;
      
    • Using bitwise XOR:

        javaCopy codea = a ^ b;
        b = a ^ b;
        a = a ^ b;
      
  5. Why might you prefer using a temporary variable for swapping in interviews?

    • It's straightforward and clear, minimizing the risk of errors in complex operations.

This guide should help you understand the concept of swapping values in Java, along with common pitfalls and alternative methods.

Did you find this article valuable?

Support YASH BLOGS by becoming a sponsor. Any amount is appreciated!