How to swap objects in Java

In this tutorial, we’ll learn how to swap objects in Java.

Swap refers to the exchange of previously assigned values. It has different approaches, such as:

  • using a temporary variable
  • appropriate addition and subtraction
  • using bitwise XOR operation

Swap objects with temporary variable:

As the name suggests, swapping takes place with a temporary variable – ‘temp’. The snippet below contains an initial class ‘A’ with initial object ‘x’. Class SMain encloses the swap object function and main function. While a1, a2 are instances of class A, which are to be swapped.

example:

  • Let a=1 and b=2, and temp is a temporary integer variable.
  • temp=a – temp is initialized with value of a
  • a=b – b’s value stored in a
  • b=temp – temp’s value (value of a) assigned to b.
class A //initial class
{ 
//initial object

    int x;
    A(int x) { this.x = x; } 
} 
class SMain 
{ 
    // Swap function 
    public static void swap(A a1, A a2) 
    { 
//temp is the temporary variable
        int temp = a1.x; 
        a1.x = a2.x; 
        a2.x = temp; 
    } 
  
    // Main section
    public static void main(String[] args) 
    { 
        A a1 = new A(183); //first object
        A a2 = new A(961); //second object
        System.out.println("Initial values of"+"\n"+"a1.x:"+a1.x+"\n"+"a2.x: "+a2.x);
        swap(a1, a2); 
        System.out.println("SWAPPED!");
        System.out.println("a1.x = " + a1.x); 
        System.out.println("a2.x = " + a2.x); 
    } 
}

Corresponding output:

Output of the following code is as follows:
Swap objects with temporary variable

Using basic addition and subtraction:

Here everything remains same, as in the above code. Class S2Main encloses the main function. The logic in swap function varies.

For  instance:

  • Assuming a=1 and b=2, so a = a+b , i.e, a=3.
  • Now, b=a-b implies b=3-2=1 (initial value of a)
  • a=a-b meaning a=3-1=2 (initial value of b)
class A
{ 
    int x; 
    A(int x) { this.x = x; } 
} 
class S2Main 
{ 
    // Swap function
    public static void swap(A a1, A a2) 
    { 
        a1.x = a1.x+a2.x; 
        a2.x = a1.x-a2.x;
        a1.x= a1.x-a2.x; 
    } 
//Main section
    public static void main(String[] args) 
    { 
        A a1 = new A(183); 
        A a2 = new A(961);
        System.out.println("Initial values of"+"\n"+"a1.x:"+a1.x+"\n"+"a2.x: "+a2.x);
        swap(a1, a2); 
        System.out.println("SWAPPED!");
        System.out.println("a1.x = " + a1.x); 
        System.out.println("a2.x = " + a2.x); 
    } 
}

Result:

For the above code, output is:
Swap objects with temporary variable

Bitwise XOR:

Technically,  this approach is very similar to the above approach. ‘^’ represents bitwise XOR. Just replace all the + and – with ‘^’ in the logic.

class A{ 
int x; 
A(int x) { this.x = x; } 
}
class S3Main {
public static void swap(A a1, A a2) 
{ //swap function
        a1.x = a1.x^a2.x; 
        a2.x = a1.x^a2.x;
        a1.x= a1.x^a2.x; 
 } //Main section
    public static void main(String[] args){ 
        A a1 = new A(183); 
        A a2 = new A(961);
        System.out.println("Initial values of"+"\n"+"a1.x:"+a1.x+"\n"+"a2.x: "+a2.x);
        swap(a1, a2); 
        System.out.println("SWAPPED!");
        System.out.println("a1.x = " + a1.x); 
        System.out.println("a2.x = " + a2.x); 
    } 
}

Outcome:

Precisely, the outcome of the above code implementation is as follows:
swap java objects Bitwise XOR

Thus, these were few methods to swap objects in Java. Prior knowledge on Java syntaxes and methods will come in handy.

Leave a Reply

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