Alex Rivera | Logout

Understanding Java generics. Type parameter conventions

Asked 2010-11-01T21:47:42.490
23

The most commonly used type parameter names are:

E - Element (used extensively by the Java Collections Framework)

K - Key

N - Number

T - Type

V - Value

S,U,V etc. - 2nd, 3rd, 4th types

I don't seem to quite understand what exactly does every letter correspond to. I understand that each letter represents just a convention but what does 2nd, 3rd and 4th type mean exactly? When should I use what? On their official tutorials website it doesn't give further information.

Edit
Report

1 Answer

1

I typically just represent the generic type as if it were a class name:

abstract class DAO<Entity, Id> {
   abstract Entity findById(Id id);
}

IMO, your code should be semantic almost to the point where it's like reading sentences instead of code. To me, this isn't very readable or semantic:

abstract class DAO<E, I> {
   abstract E findById(I id);
}

If all else just falls short, just call it what it is.

answered 2010-11-02T04:31:43.863

Your Answer