genericsGenerics in Java


Syntax

  • class MyClass<T1, T2 extends CharSequence> implements Comparable<MyClass> //...
  • interface MyListInterface<T extends Serializable> extends List<T> //...
  • public <T1, T2 extends Instant> T1 provideClone(T1 toClone, T2 instant> //...
  • public static List<CharSequence> safe(Collection<? extends CharSequence> l) { return new ArrayList<>(l);}
  • Set<String> strings = Collections.singleton("Hello world");
  • List<CharSequence> chsList = safe(strings);

Remarks

Type erasure limits reflection, though that is not JVM specific, for example Ceylon uses reified generics.

Existential type support is not necessarily supported by other languages in this form: Kotlin supports it through type projections.