3

I'm having trouble seeing the difference between when to use Generics vs Object. Right now I'm implementing quicksort and have seen examples of it done using

Generics - public static <T extends Comparable<T>> void qsort(T[] arr, int a, int b)
Objects - public static void quicksort(Object[] a, int left, int right)
Comparable - <T extends Comparable<T>> void sort(T[] a)

What really is the difference and when to use each? My goal is to make the class accessible to the largest number of data types.

Community
  • 1
  • 1
ColeKO
  • 33
  • 2
  • Is it an exercise? If not, why not use the builtin sort methods? – Eran Nov 02 '16 at 08:02
  • 2
    Possible duplicate of [Java generics T vs Object](http://stackoverflow.com/questions/5207115/java-generics-t-vs-object) – Ayoub Falah Nov 02 '16 at 08:11
  • Are you sure you quoted the right signature in your third case ("Comparable")? It is virtually the same as your "Generics" signature, whereas the link leads to a source that uses raw `Comparable`. – RealSkeptic Nov 02 '16 at 08:36
  • There isn't really any difference between the "Generics" and "Comparable" approaches in terms of the array parameter. The "Objects" approach is simply a poor approach which potentially fails at runtime, e.g. if you pass it an array `new Object[] { new Object(), new Object() }`. So, in terms of when to use the options: use either 1 or 3; don't use 2. – Andy Turner Nov 02 '16 at 08:51
  • @AyoubFalah that does not even consider type `Comparable` – ColeKO Nov 04 '16 at 21:02
  • Thanks for the accept :-) – GhostCat Mar 14 '17 at 14:37

2 Answers2

1

The conceptual thing to know about: Java arrays are covariant!

That means: you can write a method like

public void sort(Object[] data)

and use that with an array of Objects, but also with an Integer[], String[] whatever.

That has the advantage that you can write code that works "generically" for all kinds of different input.

But the problem with that is that it can lead to surprises at runtime, for example when your array contains Integer and String objects.

Thus the Java language folks decides to make generics, and more specifically collections of Generics invariant. Therefore you can't do

public void sort(List<Number> numbers)

and call that with some List<Integer>.

In that sense: when using arrays, then there isn't much sense in using generics. But: when using generics, you would (most of the time) prefer using collections over arrays! And then, you have to really know about the conceptual differences.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I do not know why you try to answer a question that is already asked and well answered. Even the old answer is confirmed by a wide range of community´s users. – Ayoub Falah Nov 02 '16 at 08:20
  • In his source code examples, he is explicitly putting up **arrays**. And when you check the answer you put up as DUP, you will find: no mentioning of arrays/variance there at all. You see, if I would have seen this as DUP, I would have voted to close it out. And then it would already be closed out ... – GhostCat Nov 02 '16 at 08:23
  • The answer to the older question is sufficient to understand the programming principles and apply it on arrays. – Ayoub Falah Nov 02 '16 at 08:30
-1

Object[] is mostly useful only if you want a mixed-type array - some Integers, some Strings etc or for supporting legacy code. In your case, this is not a good idea (i.e. why would you want a mix of types to be sorted together).

The better option is to use Generic array. But keep in mind that this would not allow you to sort primitives. You'll need to use method overloading if you want to support primitives.

Your 3rd option is pretty much the same as your first.

Vasan
  • 4,810
  • 4
  • 20
  • 39
  • The question is about explaining the difference between using Object and Generic programming in Java. – Ayoub Falah Nov 02 '16 at 08:16
  • The question was "What really is the difference and when to use each?" in the context of quick sort (which deals with arrays). So I think my answer is relevant. – Vasan Nov 02 '16 at 09:18