The Arrays class in java.util is a utility class that provides static methods to perform operations like sorting, searching, comparing, and converting arrays. It cannot be instantiated and is used only for utility purposes.
- Provides static methods such as sort(), binarySearch(), equals(), toString()
- Implicitly extends Object and is not meant to be instantiated

Class Declaration
Arrays is a final utility class in java.util package that extends Object class, which is the root of the Java class hierarchy
public final class Arrays
To use Arrays,
Arrays.<function name>;
Arrays.sort(array_name);
Why do we need Arrays class
Java provides a utility class called java.util.Arrays to help developers perform common array operations easily and efficiently. like:
- Fill an array with a particular value.
- Sort an array
- Search in an array
- And many more
Methods in Java Array ClassÂ
The Arrays class of the java.util package contains several static methods that can be used to fill, sort, search, etc in arrays. Let's take a look at methods and their implementation:
1. asList() method.
This method converts an array into a list.
import java.util.Arrays;
class Geeks{
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To convert the elements as List
System.out.println("int Array as List: "
+ Arrays.asList(intArr));
}
}
Output
Integer Array as List: [[I@19469ea2]
Explanation: This method converts an array into a fixed-size list using Arrays.asList(). It works properly with object arrays (like Integer, String), but with primitive arrays, the whole array becomes a single list element instead of individual values.
Note: asList() does not work properly with primitive arrays (like int[], char[]). It treats the entire array as a single element instead of converting each value into a list.
2. binarySearch() Method
This methods search for the specified element in the array with the help of the binary search algorithm.
import java.util.Arrays;
public class Geeks {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
Arrays.sort(intArr);
int intKey = 22;
// Print the key and corresponding index
System.out.println(
intKey + " found at index = "
+ Arrays.binarySearch(intArr, intKey));
}
}
Output
22 found at index = 3
Explanation: This method searches an element in a sorted array using binary search. If the element is found, it returns its index; otherwise, it returns a negative value indicating the insertion point.
3. binarySearch(array, fromIndex, toIndex, key, Comparator) MethodÂ
This method searches a range of the specified array for the specified object using the binary search algorithm.
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
Arrays.sort(intArr);
int intKey = 22;
System.out.println(
intKey
+ " found at index = "
+ Arrays
.binarySearch(intArr, 1, 3, intKey));
}
}
Output
22 found at index = -4
Explanation: This method searches for an element within a specified range of the array using a comparator. It is useful for custom sorting logic or partial array searching.
4. compare(array 1, array 2) MethodÂ
This method returns the difference as an integer lexicographically.
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// Get the second Array
int intArr1[] = { 10, 15, 22 };
// To compare both arrays
System.out.println("int Arrays on comparison: "
+ Arrays.compare(intArr, intArr1));
}
}
Output
Integer Arrays on comparison: 1
Explanation: This method compares two arrays lexicographically. It returns 0 if arrays are equal, a negative value if the first array is smaller, and a positive value if the first array is greater.
Methods of Array Class
Below table contains list of all methods
| Methods | Action Performed |
|---|---|
| asList() | Returns a fixed-size list backed by the specified Arrays |
| binarySearch() | Searches for the specified element in the array with the help of the Binary Search Algorithm |
| binarySearch(array, fromIndex, toIndex, key, Comparator) | Searches a range of the specified array for the specified object using the Binary Search Algorithm |
| compare(array 1, array 2) | Compares two arrays lexicographically: returns negative, 0, or positive if the first array is smaller, equal, or greater respectively. |
| copyOf(originalArray, newLength) | Copies the specified array, truncating or padding with the default value (if necessary) so the copy has the specified length. |
| copyOfRange(originalArray, fromIndex, endIndex) | Copies the specified range of the specified array into a new Arrays. |
| deepEquals(Object[] a1, Object[] a2) | Returns true if the two specified arrays are deeply equal to one another. |
| deepHashCode(Object[] a)Â | Returns a hash code based on the "deep contents" of the specified Arrays. |
| deepToString(Object[] a) | Returns a string representation of the "deep contents" of the specified Arrays. |
| equals(array1, array2) | Checks if both the arrays are equal or not. |
| fill(originalArray, fillValue) | Assigns this fill value to each index of this arrays. |
| hashCode(originalArray)Â | Returns an integer hashCode of this array instance. |
| mismatch(array1, array2)Â | Finds and returns the index of the first unmatched element between the two specified arrays. |
| parallelPrefix(originalArray, fromIndex, endIndex, functionalOperator) | Performs parallelPrefix for the given range of the array with the specified functional operator. |
| parallelPrefix(originalArray, operator) | Performs parallelPrefix for complete array with the specified functional operator. |
| parallelSetAll(originalArray, functionalGenerator) | Sets all the elements of this array in parallel, using the provided generator function. |
| parallelSort(originalArray) | Sorts the specified array using parallel sort. |
| setAll(originalArray, functionalGenerator) | Sets all the elements of the specified array using the generator function provided. |
| sort(originalArray) | Sorts the complete array in ascending order. |
| sort(originalArray, fromIndex, endIndex) | Sorts the specified range of array in ascending order. |
| sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c) | Sorts the specified range of the specified array of objects according to the order induced by the specified comparator. |
| sort(T[] a, Comparator< super T> c) | Sorts the specified array of objects according to the order induced by the specified comparator. |
| spliterator(originalArray) | Returns a Spliterator covering all of the specified Arrays. |
| spliterator(originalArray, fromIndex, endIndex)Â | Returns a Spliterator of the type of the array covering the specified range of the specified arrays. |
| stream(originalArray)Â | Returns a sequential stream with the specified array as its source. |
| toString(originalArray)Â | It returns a string representation of the array, where elements are enclosed in square brackets [] and separated by a comma and a space. |