Java Program to Swap Two Strings

1. Introduction

Swapping two strings in Java is a fundamental operation that can be done using a temporary variable or without using any additional storage by manipulating string values directly. This tutorial will demonstrate both methods to provide a comprehensive understanding of how string swapping can be implemented in Java.

Key Points

- Using a temporary variable is the most straightforward and efficient method for swapping strings.

- Swapping without a temporary variable involves string concatenation and substring operations, which can be less efficient due to string immutability in Java.

- Understanding both methods provides insight into string manipulation techniques in Java.

2. Program Steps

1. Declare and initialize two string variables.

2. Swap the strings using a temporary variable.

3. Swap the strings without using a temporary variable (for demonstration purposes only).

4. Print the values of the strings after each swap to verify correctness.

3. Code Program

public class SwapStrings {
    public static void main(String[] args) {
        // Initial strings
        String a = "Hello";
        String b = "World";

        // Using a temporary variable
        System.out.println("Using a temporary variable:");
        System.out.println("Before swap:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // Swapping with a temporary variable
        String temp = a;
        a = b;
        b = temp;

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

        // Reset values for demonstration
        a = "Hello";
        b = "World";

        // Without using a temporary variable
        System.out.println("Without using a temporary variable:");
        System.out.println("Before swap:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // Swapping without a temporary variable (not recommended)
        a = a + b;  // Step 1: concatenate a and b
        b = a.substring(0, a.length() - b.length());  // Step 2: assign b the value of the original a
        a = a.substring(b.length());  // Step 3: assign a the value of the original b

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

Output:

Using a temporary variable:
Before swap:
a = Hello
b = World
After swap:
a = World
b = Hello
Without using a temporary variable:
Before swap:
a = Hello
b = World
After swap:
a = Hello
b = World

Explanation:

1. Using a Temporary Variable:

- String temp = a;: Temporarily stores the value of a.

- a = b;: Assigns the value of b to a.

- b = temp;: Completes the swap by assigning the value stored in temp to b.

2. Without Using a Temporary Variable:

- a = a + b;: Concatenates a and b and stores the result in a.

- b = a.substring(0, a.length() - b.length());: Extracts the original a value and assigns it to b.

- a = a.substring(b.length());: Extracts the original b value and assigns it to a.

This comprehensive example demonstrates both methods to swap strings in Java, illustrating the traditional method with a temporary variable and an alternative approach using string operations.

Comments