Skip to main content

Simplify Your Tax Calculations with GST Calculator 2024: The Ultimate Tool for Businesses and Individuals

Swap two numbers in java | Using third variable | without third variable | XOR Bitwise opearation | Java tutorial for beginners .

When we learning any programming languages like C, C++, Python, PHP etc, then the "Number Swap Programming" comes first for learning. 




In this article I write the number swapping between two numbers in JAVA with 3 different algorithms

1. Creating an auxiliary memory cell in the memory ; i.e use a temporary or 3rd variable.
2. Without creating any auxiliary(additional) memory cell ; i.e without use of any 3rd variable.
3. Using exclusive OR (Bit-wise XOR) operator.

Problem Statement:
Write a program in java to swap two numbers.

Approach- 1 :  Creating an auxiliary memory cell in the memory ; i.e use a temporary or 3rd variable.

In this approach we take a extra variable that temporarily holds the value of one variable.

public class Test {


    public static void main(String[] args) {
        
        swapUsingTempVar(5,6);
    
    }


    static void swapUsingTempVar(int a, int b) {

        int temp;

        System.out.println("Before Swapping value of a is " + a + " and value of b is " + b);

        temp = a;
        a = b;
        b = temp;

        System.out.println("After Swapping value of a is " + a + " and value of b is " + b);

    }
}

Output:
Before Swapping value of a is 5 and value of b is 6
After Swapping value of a is 6 and value of b is 5

For better understanding with dry run explanation watch the video-








Approach- 2 :  Without creating any auxiliary(additional) memory cell ; i.e without use of any 3rd variable.

In this approach without any  temporary 3rd variable.

public class Test {


    public static void main(String[] args) {

        swapWithoutTempVar(5, 6);

    }

    static void swapWithoutTempVar(int a, int b) {


        System.out.println("Before Swapping value of a is " + a + " and value of b is " + b);

        a = a - b;
        b = a + b;
        a = b - a;


        System.out.println("After Swapping value of a is " + a + " and value of b is " + b);

    }


}

Output: 
Before Swapping value of a is 5 and value of b is 6
After Swapping value of a is 6 and value of b is 5

For better understanding with dry run explanation watch the video-






Approach-3. Using exclusive OR (Bit-wise XOR) operator.

Swapping the Values Using Operator. Bit-wise operators are used to perform manipulation of individual bits of a number.

public class Test {


    public static void main(String[] args) {

        swapUsingXOROperator(5, 6);

    }

    static void swapUsingXOROperator(int a, int b) {

        System.out.println("Before Swapping value of a is " + a + " and value of b is " + b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;

        System.out.println("After Swapping value of a is " + a + " and value of b is " + b);
    }

}

Output:
Before Swapping value of a is 5 and value of b is 6
After Swapping value of a is 6 and value of b is 5


So, In this article you understood the 3 ways to swap programming between 2 variable in java.




 

Comments

Popular posts from this blog