Swapping primitives and objects in Java
This post will discuss how to swap primitives and objects in Java.
We know that the most common solution to swap two integers in C/C++ is to create a utility swap() method using pointers in C or references in C++, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// C – call as swap(&a, &b) void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // C++ – call as swap(a, b) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } |
This won’t work in Java since Java is pass by value, and it is not possible to create a method for swapping two primitives in Java. This post discusses some of the workarounds/tricks for swapping integers in Java.
1. Swapping primitives
Here’s one plausible way of swapping two integers (say a and b) in Java. The idea is to assign the value of variable a to variable b after passing variable b to the swap() method. Then we simply return b from the swap() method, which gets assigned to a inside the calling method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { public static int swap(int... args) { return args[0]; } public static void main(String[] args) { int a = 5; int b = 10; a = swap(b, b = a); System.out.println("a = " + a + ", b = " + b); } } |
Output:
a = 10, b = 5
2. Swapping array elements
If values to be swapped are elements of an array, we can easily write a swap() method by passing the array indices, instead of the actual elements, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Arrays; class Main { public static void swap(int[] A, int i, int j) { int temp = A[i]; A[i] = A[j]; A[j] = temp; } public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 5 }; // index of the first element to be swapped int i = 2; // index of the second element to be swapped int j = 3; swap(A, i, j); System.out.println(Arrays.toString(A)); } } |
Output:
[1, 2, 4, 3, 5]
3. Swapping Objects
We know that Java objects are references that are passed by value. That means the only reference to the object is copied, and the object points to the exact same object as in the caller, and any changes made to the object will be reflected in the caller function. The following code does that by wrapping the integer value in a mutable object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class IntRef { public int val = 0; IntRef(int val) { this.val = val; } @Override public String toString() { return String.valueOf(val); } } class Main { public static void swap(IntRef a, IntRef b) { int temp = a.val; a.val = b.val; b.val = temp; } public static void main(String[] args) { IntRef a = new IntRef(5); IntRef b = new IntRef(10); swap(a, b); System.out.println("a = " + a + ", b = " + b); } } |
Output:
a = 10, b = 5
4. Swapping AtomicInteger
Alternately, we can replace the primitive integer by java.util.concurrent.atomic.AtomicInteger object. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.concurrent.atomic.AtomicInteger; class Main { public static void swap(AtomicInteger a, AtomicInteger b) { a.set(b.getAndSet(a.get())); } public static void main(String[] args) { AtomicInteger a = new AtomicInteger(5); AtomicInteger b = new AtomicInteger(10); swap(a, b); System.out.println("a = " + a + ", b = " + b); } } |
Output:
a = 10, b = 5
We can replace AtomicInteger class by org.apache.commons.lang3.mutable.MutableInt class from Apache Commons library.
5. Swapping List
Finally, if we need to swap two elements of a list, we can call the Collections.swap() method, which swaps the elements at the specified positions in the specified list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); // index of the first element to be swapped int i = 2; // index of the second element to be swapped int j = 3; Collections.swap(list, i, j); System.out.println(list); } } |
Output:
[1, 2, 4, 3, 5]
That’s all about swapping primitives and objects in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)