
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
Arrays are useful for storing multiple values, but simply storing data is often not enough. In real applications, we frequently need to sort data, search for values, compare arrays, or fill an array with a default value.
Instead of writing this functionality from scratch every time, Java provides the Arrays utility class in the java.util package. This class contains several built-in methods that make working with arrays easier and more efficient.
In this chapter, you will learn some of the most commonly used array methods provided by Java.
Before using array utility methods, the Arrays class must be imported.
Syntax
import java.util.Arrays;
This gives access to methods such as:
sort()toString()equals()fill()binarySearch()By default, printing an array directly does not display its elements.
Example
int[] numbers = {10, 20, 30};
System.out.println(numbers);
Output
[I@6d06d69c
This output represents the array's memory reference, not its contents.
To display the actual values, use Arrays.toString().
Example
import java.util.Arrays;
int[] numbers = {10, 20, 30};
System.out.println(Arrays.toString(numbers));
Output
[10, 20, 30]
The sort() method arranges array elements in ascending order.
Example
import java.util.Arrays;
int[] numbers = {40, 10, 30, 20};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
Output
[10, 20, 30, 40]
Explanation
The array is sorted from smallest to largest.
The equals() method compares two arrays.
Example
import java.util.Arrays;
int[] arr1 = {10, 20, 30};
int[] arr2 = {10, 20, 30};
System.out.println(Arrays.equals(arr1, arr2));
Output
true
The result is true because both arrays contain the same values in the same order.
The fill() method assigns the same value to every element in an array.
Example
import java.util.Arrays; int[] numbers = new int[5]; Arrays.fill(numbers, 100); System.out.println(Arrays.toString(numbers));
Output
[100, 100, 100, 100, 100]
Explanation
Every element in the array is replaced with 100.
The binarySearch() method searches for an element and returns its index.
Important: The array must be sorted before using this method.
Example
import java.util.Arrays;
int[] numbers = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(numbers, 30);
System.out.println(index);
Output
2
The value 30 is found at index 2.
Arrays can be copied using Arrays.copyOf().
Example
import java.util.Arrays;
int[] original = {10, 20, 30};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copy));
Output
[10, 20, 30]
A new array is created containing the same values.
The sort() method also works with String arrays.
Example
import java.util.Arrays;
String[] names = {"Rahul", "Ankit", "Priya", "Kiran"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
Output
[Ankit, Kiran, Priya, Rahul]
Strings are sorted alphabetically.
The Arrays class provides many utility methods for working with arrays. The following table contains the most commonly used methods that every Java developer should know.
When learning arrays, concentrate on these methods:
Arrays.toString()Arrays.sort()Arrays.equals()Arrays.fill()Arrays.binarySearch()Arrays.copyOf()The remaining methods become more useful when learning Streams, Functional Programming, Multithreading, and Advanced Java.
Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] prices = {500, 200, 1000, 300, 700};
Arrays.sort(prices);
System.out.println("Sorted Prices:");
System.out.println(Arrays.toString(prices));
}
}
Output
Sorted Prices: [200, 300, 500, 700, 1000]
Explanation
Arrays.sort() arranges them in ascending order.Arrays.toString() displays the array contents.Wrong
System.out.println(numbers);
Output
[I@6d06d69c
Correct
System.out.println(Arrays.toString(numbers));
Wrong
int[] numbers = {40, 10, 30, 20};
Arrays.binarySearch(numbers, 30);
The result may be incorrect.
Correct
Arrays.sort(numbers); Arrays.binarySearch(numbers, 30);
Always sort the array before using binarySearch().
Arrays class provides useful methods for working with arrays.Arrays.toString() displays array contents.Arrays.sort() sorts array elements.Arrays.equals() compares arrays.Arrays.fill() assigns a value to every element.Arrays.binarySearch() searches for an element in a sorted array.Arrays.copyOf() creates a copy of an array.