Skip to content
Advertisement

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

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

Advertisement

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) {
  // ...
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement