Search code examples
javagenericsjavadoc

What is the difference between "<T> T" and "T" return types in JavaDocs


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


Solution

  • 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) {
      // ...
    }