Java Reference
In-Depth Information
scriptive noun or noun phrase, which is appropriate when an interface is used as if it
were an abstract superclass, such as interfaces java.io.DataInput and java.io.DataOutput ; or
it may be an adjective describing a behavior, as for the interfaces Runnable and Clone-
able .
Type Variable Names
Type variable names should be pithy (single character if possible) yet evocative, and
should not include lower case letters. This makes it easy to distinguish type paramet-
ers from ordinary classes and interfaces.
Container types should use the name E for their element type. Maps should use K for
the type of their keys and V for the type of their values. The name X should be used
for arbitrary exception types. We use T for type, whenever there is not anything more
specific about the type to distinguish it. (This is often the case in generic methods.)
If there are multiple type parameters that denote arbitrary types, one should use letters
that neighbor T in the alphabet, such as S . Alternately, it is acceptable to use numer-
ic subscripts (e.g., T1 , T2 ) to distinguish among the different type variables. In such
cases, all the variables with the same prefix should be subscripted.
If a generic method appears inside a generic class, it is a good idea to avoid using the
same names for the type parameters of the method and class, to avoid confusion. The
same applies to nested generic classes.
Example 6.1-3. Conventional Type Variable Names
Click here to view code image
public class HashSet<E> extends AbstractSet<E> { ... }
public class HashMap<K,V> extends AbstractMap<K,V> { ... }
public class ThreadLocal<T> { ... }
public interface Functor<T, X extends Throwable> {
T eval() throws X;
}
When type parameters do not fall conveniently into one of the categories mentioned,
names should be chosen to be as meaningful as possible within the confines of a single
letter. The names mentioned above ( E , K , V , X , T ) should not be used for type para-
meters that do not fall into the designated categories.
Method Names
Search WWH ::




Custom Search