Search code examples
javagenericsscjp

What does a return type of <S extends CharSequence> S mean?


public <S extends CharSequence> S foo(S s){
    return null;
}

I found this method in one of the OCJP question. But I find it difficult to understand what exactly the return type <S extends CharSequence> S means. Could someone having knowledge in Java explain me what it means?


Solution

  • The definition <S extends CharSequence> means that S is a type that extends or implements CharSequence.

    Note the presence of S before foo and after it. This means that foo returns a type that either extends or implements CharSequence, and accepts an argument of the same type.