What is the difference between “ T” and “T” return types in JavaDocs

Tags: , ,



Still wrapping my head around generics so help would be appreciated.

Answer

In both cases, the return type is T.

If you see <T> before though, it means that the generic type T has been defined at the method level:

<T extends JustAnExample> T getThatThing() {
  // ...
}

If not, then it has probably been defined at the class level:

class MyClass<T extends JustAnExample> {
  T getThatThing() {
    // ...
  }
}

Or, it can technically also simply be a class named T, although those single-letter types usually refer to generics (purely by convention):

class MyClass {
  T poorlyNamedTypeYuck() {
    // ...
  }
}

Note that you don’t have to use T as the return type:

<T> void thisIsAlsoValid(T genericUsedHere, List<T> orElseWhere) {
  // ...
}


Source: stackoverflow