Java

Swap Objects in Java

While dealing with bulk data in Java, it becomes challenging for the developer to update multiple values conveniently. For instance, swapping the complex, i.e., “encoded” values with the contained entries keeping the other records and code functionalities intact. In such case scenarios, “swapping objects” in Java does wonders in lessening the hassle and saving time on the programmer’s end.

This article will demonstrate the methodologies to “swap objects” using Java.

How to “Swap Objects” in Java?

The objects in Java can be swapped using the user-defined function combined with the following:

Approach 1: Swapping Objects in Java Using the “Arithmetic Operators”

In this approach, the class objects can be swapped by performing arithmetic operations upon the created objects via the “user-defined” function:

class Swappingobj {
 public int age;
 public Swappingobj(int Age) {
  this.age = Age;
}}
public class temp {
 public static void swap(Swappingobj value1,
         Swappingobj value2) {
  value1.age = value1.age + value2.age;
  value2.age = value1.age - value2.age;
  value1.age= value1.age - value2.age;
}

According to the above “class” code, apply the following steps:

  • Firstly, define a class named “Swappingobj”.
  • In its definition, specify the stated variable.
  • Also, create a class constructor having the stated parameter.
  • In the constructor definition, refer to the specified variable and allocate it to the parameter value via “this”.
  • Note that this value will be passed later on as an argument via the “main()” method.
  • Now, define a function named “swap()” having the provided parameters referring to the objects that need to be swapped.
  • In its definition, refer to the specified main class variable, i.e., “age”.
  • After that, store the addition of the passed “age” values in the former function parameter, i.e., “value1”.
  • Similarly, return the subtraction of the passed “age” values firstly in the referred objects “value2” and then in “value1”.
  • Note: The subtraction in the last two cases will not yield the same outcome since the updated value of the parameters, i.e., “value1” and “value2” will be invoked in the second last and last computations, respectively.

Now, let’s overview the below-provided “main()” method:

public static void main(String[] args) {
  Swappingobj obj1 = new Swappingobj(18);
  Swappingobj obj2 = new Swappingobj(25);
  System.out.println("Before Swapping->");
  System.out.println("The first object is: " + obj1.age);
  System.out.println("The second object is: " + obj2.age);
  swap(obj1, obj2);
  System.out.println("After Swapping->");
  System.out.println("The first object is: " + obj1.age);
  System.out.println("The second object is: " + obj2.age);
}}

In this code snippet:

  • Create two class objects named “obj1” and “obj2” via the “new” keyword and the “Swappingobj()” constructor, respectively.
  • In the constructor parameter, pass the two “age” values that need to be swapped and display them.
  • Now, invoke the function “swap()” and pass the created class objects as its arguments.
  • This will resultantly swap the passed object values via the arithmetic operators.

Entire Code

class Swappingobj {
 public int age;
 public Swappingobj(int Age) {
  this.age = Age;
}}
public class temp {
 public static void swap(Swappingobj value1,
         Swappingobj value2) {
  value1.age = value1.age + value2.age;
  value2.age = value1.age - value2.age;
  value1.age= value1.age - value2.age;
}
 public static void main(String[] args) {
  Swappingobj obj1 = new Swappingobj(18);
  Swappingobj obj2 = new Swappingobj(25);
  System.out.println("Before Swapping->");
  System.out.println("The first object is: " + obj1.age);
  System.out.println("The second object is: " + obj2.age);
  swap(obj1, obj2);
  System.out.println("After Swapping->");
  System.out.println("The first object is: " + obj1.age);
  System.out.println("The second object is: " + obj2.age);
}}

Output

In this outcome, it can be analyzed that the object values are swapped appropriately.

Approach 2: Swapping Objects in Java Using the “Wrapper Class” and the “Assignment Operator”

In this particular approach, the created class object can be swapped with the help of an external wrapper class by making a reference to the main class and the assignment operator “=”:

class object {
 public int age;
 object(int age) {
  this.age = age;
}}
 class Wrapper {
  object x;
  Wrapper(object Inner) {
   this.x = Inner;
}}
public class Temp2 {
 static void swap(Wrapper object1, Wrapper object2) {
  object temp;
  temp = object1.x;
  object1.x = object2.x;
  object2.x = temp;
 }

In the above lines of class code, apply the following steps:

  • Recall the discussed approaches to defining a class and refer to the specified variable with the help of the class constructor.
  • After that, declare a wrapper class named “Wrapper”.
  • In its definition, refer to the main class named “object” and specify the variable “x” based on that.
  • Also, include a class constructor and assign the passed object value to the specified variable via “this”.
  • Now, similarly, define a swap function named “swap()” where the function parameters point to the object values that need to be swapped.
  • In the function definition, likewise, pass a reference of the main class to the variable “temp” and assign it the former object value wrapped in the wrapper class.
  • In the next step, allocate the value of the latter object to the former object.
  • Lastly, assign the updated value of “temp” to the latter wrapped object, i.e., “object2”.

Now, let’s proceed to the “main()” method:

 public static void main(String[] args) {
  object ob1 = new object(25);
  object ob2 = new object(30);
  Wrapper swapObject1 = new Wrapper(ob1);
  Wrapper swapObject2 = new Wrapper(ob2);
  System.out.println("Before Swapping ->\nage1: " + swapObject1.x.age +
  "\n" + "age2: "
  + swapObject2.x.age + "\n");
  swap(swapObject1, swapObject2);
  System.out.println("After Swapping ->\nage1: " + swapObject1.x.age +
  "\n" + "age2 : "
   + swapObject2.x.age);
 }}

According to the above “main()” method, apply the following steps:

  • Repeat the explained approaches for creating class objects and pass the stated “age” values as constructor parameters.
  • In the next step, create two wrapper class objects and wrap the main class objects as its constructor parameters.
  • Also, display the outcome before swapping the objects.
  • Now, access the “swap()” function and pass the wrapped class objects as its arguments.
  • This will result in swapping the “main” class objects.

Entire Code

class object {
 public int age;
 object(int age) {
  this.age = age;
}}
 class Wrapper {
  object x;
  Wrapper(object Inner) {
   this.x = Inner;
}}
public class Temp2 {
 static void swap(Wrapper object1, Wrapper object2) {
  object temp;
  temp = object1.x;
  object1.x = object2.x;
  object2.x = temp;
 }
 public static void main(String[] args) {
  object ob1 = new object(25);
  object ob2 = new object(30);
  Wrapper swapObject1 = new Wrapper(ob1);
  Wrapper swapObject2 = new Wrapper(ob2);
  System.out.println("Before Swapping ->\nage1: " + swapObject1.x.age +
  "\n" + "age2: "
  + swapObject2.x.age + "\n");
  swap(swapObject1, swapObject2);
  System.out.println("After Swapping ->\nage1: " + swapObject1.x.age +
  "\n" + "age2 : "
   + swapObject2.x.age);
 }}

Output

This output implied that the swapping is performed appropriately via the wrapper class and the user-defined function.

Conclusion

The objects in Java can be swapped using the user-defined function combined with the “Arithmetic Operators” or the “Wrapper” class and the “Assignment Operator”. The former approach swaps the objects and simply performs the computations based on the updated object values at each step. The latter approach applies the swapping by referring to the main class objects, wrapping them, and using the assignment operator in the user-defined function. This article guided about swapping objects in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.