In Java, java.util.Arrays is a utility class: a collection of static methods that do common jobs on arrays. It can print an array with toString(), sort it with sort(), search it with binarySearch(), copy it with copyOf(), fill it with fill(), and compare two arrays with equals().
The class is final and its constructor is private, so you cannot create an Arrays object; you always call Arrays.methodName(...). Most methods come in one version per primitive array type (int[], long[], double[], and so on) plus one for object arrays. The Arrays Methods index lists every guide.
Arrays have a fixed length.
Once you create new int[5], it holds exactly five elements forever. No Arrays method can make that array longer or shorter: copyOf() and copyOfRange() return a new array, and asList() gives a list whose size is also fixed. For a growable collection, use an ArrayList.
Syntax
Java Syntax
Import the class, then pass your array to a static method:
Java
import java.util.Arrays;
int[] a = {3, 1, 2};
Arrays.sort(a); // a is now 1, 2, 3
String text = Arrays.toString(a); // "[1, 2, 3]"
int[] b = Arrays.copyOf(a, 5); // [1, 2, 3, 0, 0]
boolean same = Arrays.equals(a, b); // false (lengths differ)
// Arrays x = new Arrays(); // does not compile: private constructor
Methods that rearrange or overwrite elements, such as sort(), fill(), and setAll(), change the array you pass in and return void. Methods that build something new, such as copyOf() and toString(), leave the original alone.
Reference
Array vs Arrays vs ArrayList
Three names that look alike but mean different things:
Name
What it is
Keep in mind
int[], String[]
A plain array: fixed length, fast index access, can hold primitives
No useful toString(), equals(), or hashCode() of its own
java.util.Arrays
A helper class with static methods that work on arrays
A resizable list object backed by an internal array
Holds objects only, so numbers become Integer, Double, and so on
Reference
Why Printing an Array Looks Strange
Arrays inherit toString(), equals(), and hashCode() from Object. So System.out.println(nums) prints something like [I@1b6d3586 (a type code plus a hash that changes between runs), nums1.equals(nums2) is true only for the very same array object, and nums.hashCode() ignores the contents. Arrays provides the content-based versions:
java.util.Arrays is a utility class with static methods for arrays: toString, sort, binarySearch, copyOf, fill, equals, hashCode, asList, stream, and more. It has been part of Java since version 1.2, and newer versions keep adding methods.
No. The class has a private constructor and every method is static, so you call methods on the class name, for example Arrays.sort(a). Writing new Arrays() does not compile.
Arrays is a helper class for plain arrays such as int[] and String[], which have a fixed length. ArrayList is a list object that grows and shrinks as you add and remove elements. Arrays.asList and new ArrayList<>(Arrays.asList(...)) connect the two.
Use Arrays.toString(a) for a one-dimensional array and Arrays.deepToString(a) for nested arrays. Printing the array directly shows something like [I@1b6d3586, which is a type code and a hash, not the contents.
Not directly: the Comparator overloads only accept object arrays. Either sort an Integer[] with Collections.reverseOrder(), or sort the int[] ascending and then reverse it with a simple loop.