33

I was going through Java documentation, and I learned that methods in the Arrays class in Java are all static. I don't really understand the reason behind why they made it static.

For example, the following code violates the OO approach, because if I have a type, 'X', then all the methods which acts on it should be inside it:

int[] a = {34, 23, 12};
Arrays.sort(a);

It would be better if they have implemented the following way:

int[] a = {34, 23, 12};
a.sort();

Can anyone explain me a bit on this?

1

8 Answers 8

41

In Java there is no way to extend the functionally of an array. Arrays all inherit from Object but this gives very little. IMHO This is a deficiency of Java.

Instead, to add functionality for arrays, static utility methods are added to classes like Array and Arrays. These methods are static as they are not instance methods.

13
  • 6
    But java arrays have a length property which doesn't come from Object. So why could other properties or even methods not be added?
    – Asaph
    Aug 23, 2016 at 5:03
  • 9
    @Asaph the length read only property is defined in the JLS and has it's own byte code instruction. arraylength Extending this is not as simple as adding methods to a class. Aug 23, 2016 at 5:05
  • 6
    I agree that this is a deficiency in Java's design. At the very least, the instance versions of equals(), hashCode(), and toString() should've been implemented properly for array classes. But they refuse to do this because of stringent backwards compatibility policy.
    – user6732794
    Aug 23, 2016 at 5:07
  • 4
    @friendlydog you can add to toString() by changing the implementation of Object.tostring() vanillajava.blogspot.com.au/2016/03/… Aug 23, 2016 at 5:08
  • 2
    While I agree with the 2nd half of this answer, I find the first to be incorrect. Oracle (through the JCP) could have updated the language specification to add a sort method to array. However, in Java arrays are much more primitive than arrays in many of the newer languages, so I do think adding a sort method wouldn't be a good idea, but if they were motivated enough they could do it. Using utility classes makes more sense in this case.
    – Robert
    Aug 23, 2016 at 17:39
20

Good observation. Observe also that not every array can be sorted. Only arrays of primitives and Objects which implement the Comparable interface can be sorted. So a general sort() method that applies to all arrays is not possible. And so we have several overloaded static methods for each of the supported types that are actually sortable.

Update:

@Holger correctly points out in the comments below that one of the overloaded static methods is indeed Arrays.sort(Object[]) but the docs explicitly state:

All elements in the array must implement the Comparable interface.

So it doesn't work for Objects that don't implement Comparable or one of its subinterfaces.

8
  • 1
    Actually, there are methods for primitive types and there is Arrays.sort(Object[]). Which array type do you have in mind, when saying that there is no general sort() method? Note that the mentioned variant even works flawlessly, when using a non-comparable array type but comparable elements, e.g. Object[] array={ "foo", "bar" }; Arrays.sort(array);
    – Holger
    Aug 23, 2016 at 16:30
  • The docs for Arrays.sort(Object[]) explicitly say "All elements in the array must implement the Comparable interface.". This is probably a carry over from the pre-generics era. Also, java.lang.String does implement the Comparable interface.
    – Asaph
    Aug 23, 2016 at 16:35
  • 1
    Yes, as I said, the elements are comparable, but the array type is Object[] which is not a subtype of Comparable[]. But, as said, this constraint is not enforced by the signature. Your answer creates the impression that the provided methods kinda sort out arrays that are not comparable, whereas in fact you can pass in any array and will only notice at runtime, when the elements aren’t comparable (and this hasn’t changed with generics).
    – Holger
    Aug 23, 2016 at 16:46
  • 1
    Arrays.sort(Comparable[]) would not work because generics and arrays don't usually play nicely together: methods taking arrays must generally accept reifiable types, which generics are not due to type erasure.
    – user439793
    Aug 23, 2016 at 19:43
  • 1
    @Asaph: of course, signatures had to stay the same for backwards compatibility when Generics were introduced. E.g. note that Comparable now has the generic signature Comparable<T> rather than Comparable<T extends Comparable<T>> (like with Enum<E extends Enum<E>>). The reason is that, for whatever reason, the original pre-Generics signature of the compare method is compare(Object) rather than compare(Comparable). Hence, the generic signature had to be defined in a way that the erased signature of compare(T) stays compare(Object). Tricky.
    – Holger
    Aug 24, 2016 at 9:06
13

First of all, Arrays is an utility class, which does exactly that: exposes static methods. It is separate from any arr[] instances and has no OO relation to it. There are several classes like that, like Collections or various StringUtils.

Arrays are collections, they are used to store data. Arrays.sort() is an algorithm which sorts the collection. There may be many other algorithms which sort data in different way, all of them would be used in the same way: MyAlgorithm.doSthWithArray(array). Even if there was a sort() method on an array (it would then have to be a SortableArray, because not all Objects can be sorted automatically), all other algorithms would have to be called the old way anyway. Unless there was a visitor pattern introduced... But that makes things too complicated, hence, there is no point.

For a java Collection there's Collections.sort(), even in C++ there is std::sort which works similarly, as does qsort in C . I don't see a problem here, I see consistency.

9
  • 2
    Note that in Java 8 there is a List.sort instance method. Aug 23, 2016 at 6:59
  • 1
    Agreed, it's not syntactic sugar, let's call it convenience method. Which is quite inconvenient because it may introduce a runtime exception where normally a compile-time check would locate the error.
    – Dariusz
    Aug 23, 2016 at 9:53
  • 2
    This is the proper answer that diagnoses the OP's (understandable) misunderstanding: Arrays is not the type of an array, hence its static methods could not be member functions of arrays without altering the language. This is different from wrapper types like Integer etc. (These wrappers are, btw., hybrids in that they are value-carrying objects as well as utility classes for the underlying primitives by providing static methods like static String toString(int i).) Aug 23, 2016 at 13:43
  • 2
    @Dariusz: it’s not just a convenience method as, unlike the static method in Collections, implementations can now override the sort method, which the widely used ArrayList does for efficiency, whereas lists returned by synchronizedList do that to provide thread safety which wasn’t there in previous releases. Regarding type safety, trying to sort lists naturally without a declared comparable element type always worked via Collections.sort(list, null). If you don’t like it, you can use list.sort(Comparator.naturalOrder()) which does the check exactly like Collections.sort(list)
    – Holger
    Aug 23, 2016 at 16:41
  • 1
    @Dariusz: yes, but as said, you can bypass that check by using Collections.sort(list, null). So you have the choice of using the checked comparator-less Collections.sort(list) or the unchecked Collections.sort(list, null). Likewise, you have a choice when calling sort on a list, use the checked list.sort(Comparator.naturalOrder()) or the unchecked list.sort(null). Unfortunately, it is impossible to offer a checked comparator-less list.sort().
    – Holger
    Aug 24, 2016 at 9:28
3

Static Methods are sometimes used for utility purpose. So Arrays is utility class for general purpose array operations. Similarly, Collections is also Util class where utility methods are given.

2

Arrays are kind of like second-class generics. When you make an array it makes a custom class for the array type, but it's not full featured because they decided how arrays would work before they really fleshed out the language.

That, combined with maintaining backwards compatibility, means that Arrays are stuck with an archaic interface.

It's just an old part of the API.

2

An array is not an object which stores state, beyond the actual values of int the array. In other words, it's just a "dumb container". It doesn't "know" any behaviour.

A utility class is a class which has just public static methods which are stateless functions. Sorting is stateless because there's nothing remembered between calls to that method. It runs "standalone", applying its formula to whatever object is passed in, as long as that object is "sortable". A second instance of an Arrays class would have behaviour no different, so just have the one static instance.

As Dariusz pointed out, there are different ways of sorting. So you could have MyArrays.betterSort(array) as well as Arrays.sort(array).

If you wanted to have the array "know" how best to sort its own members, you'd have to have your own array class which extends an array.

But what if you had a situation where you wanted different sorting on different times on the the same array? A contrived example, maybe, but there are plenty of similar real-world examples.

And now you're getting complicated. Maybe an array of type T sort differently than type S ....

It's made simple with a static utility and the Comparator<T> interface.

1

For me this is the perfect solution. I have an array, and I have a class, Arrays, which operates over the data in the array. For example, you may want to hold some random numbers and you will never want to sort or any other utility method you will receive behavior which you don't want. That's why in code design it is good to separate data from the behavior.

You can read about the single responsibility principle.

1
  • Just wondering (never worked with Java): You might also have a string "abc" that you only need to print, yet it comes with a bunch of instance methods. Aren't you essentially getting behavior which you don't want? Or is there some difference that I'm missing (because I don't know the language)?
    – ROAL
    Aug 24, 2016 at 7:01
1

The Arrays class contains methods that are independent of state, so therefore they should be static. It's essentially a utility class.

While OOP principles don't apply, the current way is clearer, concise, and more readable since you don't have to worry about polymorphism and inheritance. This all reduces scope, which ultimately reduces the chances that you screw something up.

Now, you may ask yourself "Why can't I extend the functionality of an array in Java?". A nice answer is that this introduces potential security holes, which could break system code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.