Reverse an Array in Java
Program to reverse the elements of an array.
Last updated: 11 January 2026
Method 1: In-place Reversal
Swapping elements from both ends.
Main.javaRun in Compiler →
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Original Array: ");
printArray(arr);
int start = 0;
int end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
System.out.println("Reversed Array: ");
printArray(arr);
}
static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}Output:
Original Array: 1 2 3 4 5 Reversed Array: 5 4 3 2 1
Explanation
Swap first and last elements, then second and second-last, moving inwards.
Frequently Asked Questions
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler