Updated October 2026. Every example was run on Java 21.
To swap two arrays in Java, you can swap the array references, swap the elements one by one with a loop, or copy the contents with System.arraycopy(). Which one is right depends on one question: do you need the arrays themselves to change, or only the variables that point to them?
This post shows all three ways with real output. It also covers a mistake that trips up many beginners: a swap() method that looks right but does nothing.
Which way should you use?
| Way | What it does | Works with different lengths? | Cost |
|---|---|---|---|
| Swap the references | Two variables point at each other’s array | Yes | O(1) |
| Swap elements in a loop | Exchanges values at each index | No, lengths must match | O(n) |
System.arraycopy() |
Copies the contents in bulk | No, lengths must match | O(n) |
1. Swap the references
An array variable in Java does not hold the numbers. It holds a reference to them. So the quickest way to swap two arrays is to swap the references, exactly like you swap two int variables with a temporary one.
import java.util.Arrays;
public class SwapReferences {
public static void main(String[] args) {
int[] first = {1, 5, 6, 9, 8};
int[] second = {5, 6, 4, 2, 8, 7}; // different length is fine
int[] temp = first;
first = second;
second = temp;
System.out.println("first = " + Arrays.toString(first));
System.out.println("second = " + Arrays.toString(second));
}
}
Output:
first = [5, 6, 4, 2, 8, 7]
second = [1, 5, 6, 9, 8]
No element is copied, so this takes the same time for an array of 5 numbers or 5 million. Besides, it also works when the two arrays have different lengths.
2. Why a swap() method does not work
Most people try to move this code into a method. Here is what happens.
WRONG
import java.util.Arrays;
public class WrongSwap {
static void swap(int[] a, int[] b) {
int[] temp = a;
a = b;
b = temp;
}
public static void main(String[] args) {
int[] first = {1, 5, 6};
int[] second = {5, 6, 4};
swap(first, second);
System.out.println("first = " + Arrays.toString(first));
System.out.println("second = " + Arrays.toString(second));
}
}
Output:
first = [1, 5, 6]
second = [5, 6, 4]
Nothing changed. That is because Java always passes arguments by value, and for an array that value is a copy of the reference. Inside swap(), the variables a and b are swapped, but first and second in main still point where they did before.
The fix is to change what is inside the arrays. After all, both the method and the caller see the same array objects. That leads to the next way.
3. Swap the elements with a loop
This version exchanges the values at each index. As a result, the caller really does see the change.
RIGHT
import java.util.Arrays;
public class SwapContents {
static void swap(int[] a, int[] b) {
if (a.length != b.length) {
throw new IllegalArgumentException(
"Arrays must have the same length: " + a.length + " vs " + b.length);
}
for (int i = 0; i < a.length; i++) {
int temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
public static void main(String[] args) {
int[] first = {1, 5, 6};
int[] second = {5, 6, 4};
swap(first, second);
System.out.println("first = " + Arrays.toString(first));
System.out.println("second = " + Arrays.toString(second));
try {
swap(first, new int[]{1, 2});
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
first = [5, 6, 4]
second = [1, 5, 6]
Error: Arrays must have the same length: 3 vs 2
The length check matters. Otherwise, a shorter second array would fail halfway through with an ArrayIndexOutOfBoundsException, and the first array would already be half changed.
You may see versions that use a third “buffer” array. However, you do not need one. A single int temp is enough, because you only hold one value at a time.
4. Swap with System.arraycopy()
System.arraycopy() copies a block of elements in one call, and it is usually faster than a hand-written loop for large arrays.
import java.util.Arrays;
public class SwapArrayCopy {
static void swap(int[] a, int[] b) {
int[] temp = a.clone();
System.arraycopy(b, 0, a, 0, a.length);
System.arraycopy(temp, 0, b, 0, b.length);
}
public static void main(String[] args) {
int[] first = {1, 5, 6, 9, 8};
int[] second = {5, 6, 4, 2, 8};
swap(first, second);
System.out.println("first = " + Arrays.toString(first));
System.out.println("second = " + Arrays.toString(second));
}
}
Output:
first = [5, 6, 4, 2, 8]
second = [1, 5, 6, 9, 8]
This version needs an extra array, because a.clone() keeps a copy of the first array while you overwrite it. For that reason, the loop is the better choice when memory is tight.
Swap two arrays read from user input
Sometimes you need to read both arrays from the keyboard. Here is a short version. Because the arrays can have different sizes, it first asks for each size and then swaps the references.
import java.util.Arrays;
import java.util.Scanner;
public class SwapFromInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Size of the first array: ");
int[] first = new int[sc.nextInt()];
System.out.print("Size of the second array: ");
int[] second = new int[sc.nextInt()];
System.out.println("Enter " + first.length + " elements for the first array:");
for (int i = 0; i < first.length; i++) first[i] = sc.nextInt();
System.out.println("Enter " + second.length + " elements for the second array:");
for (int i = 0; i < second.length; i++) second[i] = sc.nextInt();
int[] temp = first;
first = second;
second = temp;
System.out.println("After swapping");
System.out.println("first = " + Arrays.toString(first));
System.out.println("second = " + Arrays.toString(second));
}
}
With the input 5, 6, 1 5 6 9 8 and 5 6 4 2 8 7, the program prints:
After swapping
first = [5, 6, 4, 2, 8, 7]
second = [1, 5, 6, 9, 8]
Notice that Arrays.toString() puts commas between the numbers. Printing each number with no separator gives 15698, which is hard to read.
Common mistakes
- Writing a
swap(a, b)method that swaps references. The caller never sees the change. Swap the contents instead, or swap the references where the variables live. - Skipping the length check. Otherwise, if the lengths differ, you get an
ArrayIndexOutOfBoundsExceptionpartway through the swap. - Using
==to compare the arrays afterwards. It compares references, not contents. UseArrays.equals()instead. - Expecting
clone()to copy nested arrays. For a 2D array,clone()copies only the outer array.
Summary
To swap two arrays in Java, you can swap the references when you own both variables, or swap the contents when another method needs to see the change. In short, references are the fastest, and a loop or System.arraycopy() is the safe choice when the arrays must keep their identity.
References
Related posts
- How to Rotate an Array to Left direction based on user input
- Bubble Sort In Java
- How to Rotate Elements in List
How can we swap 133967 in java