2

I'm confused by this syntax because at first it appears as though the function should return <E> but then I see void. What is the purpose of the <E> prior to the void?

Also, I'm familiar with bounding and generics, but I've never actually seen something bounded this way. What does this syntax mean Comparator<? super E>?

Here is a sample function:

private <E> void sort(E[] array, Comparator<? super E>  cmp) {
   qsort(array, 0, array.length - 1, cmp);
}
nullByteMe
  • 6,141
  • 13
  • 62
  • 99
  • 2
    "What is the purpose of the `` prior to the void?" It shows it's a generic method. "What does this syntax mean `Comparator super E>`?" See https://docs.oracle.com/javase/tutorial/java/generics/lowerBounded.html. – Jon Skeet Jul 26 '15 at 14:10
  • https://docs.oracle.com/javase/tutorial/java/generics/methods.html – JB Nizet Jul 26 '15 at 14:11
  • @JonSkeet thanks, Jon. Is that syntax optional? – nullByteMe Jul 26 '15 at 14:11
  • @inquisitor: Not when you're declaring a generic method, no. – Jon Skeet Jul 26 '15 at 14:11
  • 1
    @inquisitor when you're wondering about some feature of Java, like generics, just google for "Java tutorial" (so in this case "Java generics tutorial"), and the first result is usually the right one. Java tutorials contain detailed explanations, full of examples. – JB Nizet Jul 26 '15 at 14:13
  • 1
    @JBNizet thanks for that. I wasn't sure initially how to google for the answer mainly because it was sytnax related. But you're right, will do so in the future. – nullByteMe Jul 26 '15 at 14:14

3 Answers3

3

The first <E> is not a type - it is a type constraint.

Bear in mind that Java implements generics via type erasure - this means that the runtime type signature of this method is

private void sort(Object[] array, Comparator cmp)

(by removing everything in between the <>s) so your method has return type void.

What <E> does is to say that the types of the input array and comparator are related: the comparator needs to be able to compare 'things' of type E, but it actually doesn't have to only handle things of the exact type E.

This is what <? super E> does: for example, you could have a Comparator<CharSequence>, and then use that to sort String[], since String is a subclass of CharSequence.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
3

at first it appears as though the function should return <E> but then I see void

The <E> part there is a syntax for supplying generic methods their type argument. The actual return type is void.

What does this syntax mean Comparator<? super E>

This means that the comparator may be for E or for any of its superclasses.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2
Comparator<? super E>  

? is the wildcard character and the lower bound for this is E.

for example:

private <E> void sort(E[] array, Comparator<? super E>  cmp)

if you are passing

qsort(array, 0, array.length - 1, cmp);

if array is Number[] then lower bound of "?" is set to java.lang.Number ie "?" can be anything which is a super class of Number.

Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37