Generic Types

A generic type is a class or interface that introduces a family of parameterized types by declaring a formal type parameter list (a comma-separated list of type parameter names between angle brackets). This syntax is expressed as follows:

class identifier<formal_type_parameter_list> {} interface identifier<formal_type_parameter_list> {}

For example, List<E> is a generic type, where List is an interface and type parameter E identifies the list's element type. Similarly, Map<K, V> is a generic type, where Map is an interface and type parameters K and V identify the map's key and value types.

NOTE: When declaring a generic type, it is conventional to specify single uppercase letters as type parameter names. Furthermore, these names should be meaningful. For example, E indicates element, T indicates type, K indicates key, and V indicates value. If possible, you should avoid choosing a type parameter name that is meaningless where it is used. For example, List<E> means list of elements, but what does List<S> mean?

Parameterized types instantiate generic types. Each parameterized type replaces the generic type's type parameters with type names. For example, List<Employee> (List of

Employee) and List<String> (List of String) are examples of parameterized types based on List<E>. Similarly, Map<String, Employee> is an example of a parameterized type based on Map<K, V>.

The type name that replaces a type parameter is known as an actual type argument. Generics supports five kinds of actual type arguments:

■ Concrete type: The name of a class or interface is passed to the type parameter. For example, List<Employee> employees; specifies that the list elements are Employee instances.

■ Concrete parameterized type: The name of a parameterized type is passed to the type parameter. For example, List<List<String>> nameLists; specifies that the list elements are lists of strings.

■ Array type: An array is passed to the type parameter. For example, List<String[]> countries; specifies that the list elements are arrays of Strings, possibly city names.

■ Type parameter: A type parameter is passed to the type parameter. For example, given class declaration class X<E> { List<E> queue; }, X's type parameter E is passed to List's type parameter E.

■ Wildcard: The ? is passed to the type parameter. For example, List<?> list; specifies that the list elements are unknown. You will learn about this type parameter later in the chapter, in "The Need for Wildcards" section.

A generic type also identifies a raw type, which is a generic type without its type parameters. For example, List<Employee>'s raw type is List. Raw types are nongeneric and can hold any Object.

NOTE: Java allows raw types to be intermixed with generic types to support the vast amount of legacy code that was written prior to the arrival of generics. However, the compiler outputs a warning message whenever it encounters a raw type in source code.

Was this article helpful?

0 0

Post a comment