1

I always thought that any array type is like a different class but it seems like any reference type arrays are also considered an object of Object[] and their superclasses' array form. (Is this exactly right?)

Consider the following array

Integer[] numbers = { 20, 15, 10, 5 };

When I pass this array to Arrays.sort which expects an Object[] value, it works fine. But an Object doesn't have any method to compare, how does it know which compare method to use?

2 Answers 2

4

Integer implements Comparable<Integer> which defines a natural ordering on Integers. Calling sort without a Comparator relies on the elements of the array to implement Comparable, and uses the compareTo method of that interface.

Sign up to request clarification or add additional context in comments.

4 Comments

It's waiting Object[] and I'm passing an Integer array. How does it know if i passed an array of integer or student or StringBuilder or any other class? I mean hasn't it lost type information?
@InsignificantPerson If you read the Java doc of that method, you'll see that All elements in the array must implement the Comparable interface and also they must be mutually comparable. If you are passing to this method an array containing elements that don't fulfill these requirements, you'll get exceptions. It doesn't care what you pass. It attempts to cast each element that required comparison to Comparable and calls the compareTo method, passing to it another object.
thx for the answer but still couldn't get a confirmation about whether every subclass' array type is a subclass of its superclass's array type. For example Cat extends Animal. Does Cat[] extend Animal[] ? (I know it can hold references of Cats but i ask about the array object itself)
@InsignificantPerson Since you can assign a Cat[]` to an Animal[] variable, I think you can say that Cat[] extends Animal[]. For example : Cat[] ca = new Cat[5]; Animal[] aa = ca; works.
0

Arrays.sort is used for primitive types and the Object as well.

As per the array elements type those are being sorted by using sort1 but these sort1 methods are private.

  1. If you are tying to sort int array. So you will use method sort(int[] a). It will call the private method sort1:

    private static void sort1(int x[], int off, int len) {...}

And this method description is Sorts the specified sub-array of integers into ascending order.

  1. If you are going to sort Object it will call mergeSort method.

But When you look at Integer class file, it implements Comparable<Integer> (Remember although it's subclass of Object):

public final class Integer extends Number implements Comparable<Integer>

So here you don't want to use the Arrays.sort method. By using the compareTo(Integer anotherInteger) method the array can be sorted.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.