Clean • Professional
When working with arrays, Java provides a powerful helper class — java.util.Arrays.
This class contains static utility methods that make it easier to perform common array operations like:
Using these methods helps write cleaner, faster, and more readable code.

Arrays.toString() – Convert Array to Readable StringNormally, printing an array directly like this:
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers);
prints something like:
[I@15db9742
That’s the array’s memory reference, not its content.
To print the actual elements, use Arrays.toString():
import java.util.Arrays;
public class ToStringExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Array: " + Arrays.toString(numbers));
}
}
Output:
Array: [1, 2, 3, 4, 5]
Notes:
Arrays.deepToString() instead.Example (Multidimensional):
int[][] matrix = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(matrix));
Output:
[[1, 2], [3, 4]]
Arrays.equals() – Compare Two ArraysThe equals() method checks whether two arrays contain the same elements in the same order.
import java.util.Arrays;
public class EqualsExample {
public static void main(String[] args) {
int[] arr1 = {10, 20, 30};
int[] arr2 = {10, 20, 30};
int[] arr3 = {30, 20, 10};
System.out.println(Arrays.equals(arr1, arr2)); // true
System.out.println(Arrays.equals(arr1, arr3)); // false
}
}
Output:
true
false
Notes:
true only if both arrays have equal length and elements.Arrays.deepEquals().Example (Nested Arrays):
String[][] names1 = {{"Aman", "Ravi"}, {"Neha", "Tina"}};
String[][] names2 = {{"Aman", "Ravi"}, {"Neha", "Tina"}};
System.out.println(Arrays.deepEquals(names1, names2)); // true
Arrays.fill() – Fill Array with a Specific ValueUsed to initialize or reset all elements of an array with a single value.
import java.util.Arrays;
public class FillExample {
public static void main(String[] args) {
int[] marks = new int[5];
Arrays.fill(marks, 100);
System.out.println("Filled Array: " + Arrays.toString(marks));
}
}
Output:
Filled Array: [100, 100, 100, 100, 100]
Notes:
Arrays.fill(array, fromIndex, toIndex, value)Example (Range Fill):
int[] scores = {10, 20, 30, 40, 50};
Arrays.fill(scores, 1, 4, 99);
System.out.println(Arrays.toString(scores));
Output:
[10, 99, 99, 99, 50]
Arrays.sort() – Sort Array in Ascending OrderThe Arrays.sort() method is used to sort elements of an array in ascending order by default.
import java.util.Arrays;
public class SortExample {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1, 3};
Arrays.sort(numbers);
System.out.println("Sorted Array: " + Arrays.toString(numbers));
}
}
Output:
Sorted Array: [1, 2, 3, 5, 8]
Notes:
Collections.reverseOrder() (works for Integer[], not int[]).Descending Example:
import java.util.*;
public class DescendingExample {
public static void main(String[] args) {
Integer[] numbers = {5, 2, 8, 1, 3};
Arrays.sort(numbers, Collections.reverseOrder());
System.out.println("Descending Order: " + Arrays.toString(numbers));
}
}
deepToString() and deepEquals() for nested arrays.Arrays.fill() is useful for default initialization.Arrays.sort() is optimized and very fast for large arrays.Q1. Can I use Arrays.sort() on a List?
→ No, Arrays.sort() works only on arrays. Use Collections.sort() for Lists.
Q2. What happens if I use Arrays.equals() on arrays of different lengths?
→ It immediately returns false.
Q3. Does Arrays.fill() work on multidimensional arrays?
→ No, it fills only one-dimensional arrays directly. For nested arrays, loop through subarrays.
Q4. What is the time complexity of Arrays.sort()?
→ Dual-Pivot Quicksort → O(n log n) on average.
Q5. Can I customize sorting order in Arrays.sort()?
→ Yes, with a custom Comparator for object arrays.