Generics in Java

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Generics are a facility of generic programming that were added to the Java programming language in 2004 within J2SE 5.0. They allow "a type or method to operate on objects of various types while providing compile-time type safety."[1] This feature specifies the type of objects stored in a Java Collection. In the year 1998, Philip Wadler created Generic Java, an extension to the Java language to support generic types.[2] Generic Java was incorporated, with the addition of wildcards, into the official Java language version J2SE 5.0.

Hierarchy and classification.[edit]

According to Java Language Specification:[3]

  • A type variable is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations.
  • A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. It defines one or more type variables that act as parameters. A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime.
  • An interface is generic if it declares one or more type variables. These type variables are known as the type parameters of the interface. It defines one or more type variables that act as parameters. A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at runtime.
  • A method is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or interface.
  • A constructor can be declared as generic, independently of whether the class that the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list is identical to a type parameter list of a generic class or interface.

Motivation[edit]

The following block of Java code illustrates a problem that exists when not using generics. First, it declares an ArrayList of type Object. Then, it adds a String to the ArrayList. Finally, it attempts to retrieve the added String and cast it to an Integer.

  List v = new ArrayList();
  v.add("test");
  Integer i = (Integer)v.get(0);        // Run time error

Although the code compiles without error, it throws a runtime exception (java.lang.ClassCastException) when executing the third line of code. This type of problem can be avoided by using generics and is the primary motivation for using generics.

Using generics, the above code fragment can be rewritten as follows:

  List<String> v = new ArrayList<String>();
  v.add("test");
  Integer i = v.get(0); // (type error)  Compile time error

The type parameter String within the angle brackets declares the ArrayList to be constituted of String (a descendant of the ArrayList's generic Object constituents). With generics, it is no longer necessary to cast the third line to any particular type, because the result of v.get(0) is defined as String by the code generated by the compiler.

Compiling the third line of this fragment with J2SE 5.0 (or later) will yield a compile-time error because the compiler will detect that v.get(0) returns String instead of Integer. For a more elaborate example, see reference.[4]

Here is a small excerpt from the definitions of the interfaces List and Iterator in package java.util:

  public interface List<E> { 
    void add(E x);
    Iterator<E> iterator();
  }
  public interface Iterator<E> { 
    E next();
    boolean hasNext();
  }

Type wildcards[edit]

A type argument for a parameterized type is not limited to a concrete class or interface. Java allows the use of type wildcards to serve as type arguments for parameterized types. Wildcards are type arguments in the form "?", possibly with an upper or lower bound. Given that the exact type represented by a wildcard is unknown, restrictions are placed on the type of methods that may be called on object of the parameterized type.

As an example of an unbounded wildcard, List<?> indicates a list which has an unknown object type. Methods which take such a list as a parameter will accept any type of list as argument. Reading from the list will return objects of type Object, and adding non-null elements to the list is not allowed, since the element type is not known.

To specify the upper bound of a type wildcard, the extends keyword is used, which indicates that the type argument is a subtype of the bounding class. So List<? extends Number> means that the given list contains objects of some unknown type which extends the Number class. For example, the list could be List<Float> or List<Number>. Reading an element from the list will return a Number, while adding non-null elements is once again not allowed.

The use of wildcards above adds flexibility since there is not any inheritance relationship between any two parameterized types with concrete type as type argument. Neither List<Number> nor List<Integer> is a subtype of the other, even though Integer is a subtype of Number. So, any method that takes List<Number> as a parameter does not accept an argument of List<Integer>. If it did, it would be possible to insert a Number that is not an Integer into it, which violates type safety. Here is sample code that explains the contradiction it brings if List<Integer> were a subtype of List<Number>:

List<Integer> ints = new ArrayList<Integer>();
ints.add(2);
List<Number> nums = ints;  // valid if List<Integer> were a subtype of List<Number> according to substitution rule. 
nums.add(3.14);  
Integer x=ints.get(1); // now 3.14 is assigned to an Integer variable!

The solution with wildcards works because it disallows operations that would violate type safety.

List<? extends Number> nums = ints;  // it is OK
nums.add(3.14); // it is prohibited

To specify the lower bounding class of a type wildcard, the super keyword is used. This keyword indicates that the aforementioned parameterized type is with a type argument which is a super-type of said bounding type. So, List<? super Number> could represent List<Number> or List<Object>. Reading from a list defined as List<? super Number> returns elements of type Object. Adding to such a list requires elements of type Number or any sub type of Number.

The mnemonics PECS (Producer Extends, Consumer Super) from the book Effective Java by Joshua Bloch gives an easy way to remember when to use wildcards (corresponding to Covariance and Contravariance) in Java.

Generic class definitions[edit]

Here is an example of a generic class:

public class Entry<K, V> {
 
  private final K key;
  private final V value;
 
  public Entry(K k,V v) {  
    key = k;
    value = v;   
  }
 
  public K getKey() {
    return key;
  }
 
  public V getValue() {
    return value;
  }
 
  public String toString() { 
    return "(" + key + ", " + value + ")";  
  }
}

Note that the above class is for illustration of Java generics only.

This generic class can be used in the following way:

Entry<String, String> grade440 = new Entry<String, String>("mike", "A");
Entry<String, Integer> marks440 = new Entry<String, Integer>("mike", 100);
System.out.println("grade: " + grade440);
System.out.println("marks: " + marks440);

Generic method definitions[edit]

Here is an example of a generic method using the generic class above:

public static <T> Entry<T,T> twice(T value) {
   return new Entry<T,T>(value, value);
}

Note: If we remove the first <T> in the above method, we will get compilation error (cannot find symbol 'T') since it represents the declaration of the symbol.

In many cases the user of the method need not indicate the type parameters, as they can be inferred:

Entry<String, String> pair = twice("Hello");

The parameters can be explicitly added if needed:

Entry<String, String> pair = this.<String>twice("Hello");

Note that you cannot use primitive types, ex:

Entry<int, int> pair; // this fails. You have to use Integer instead.

There is also the possibility to create generic methods based on given parameters.

public <T> T[] toArray(T... elements) {
   return elements;
}

In such cases you can't use primitive types either, ex:

   Integer[] array = toArray(1,2,3,4,5,6);

Generics in throws clause[edit]

Although exceptions themselves cannot be generic, generic parameters can appear in a throws clause:

public <T extends Throwable> void throwMeConditional
      (boolean conditional, T exception) throws T {
   if(conditional)
     throw exception;
}

Problems with type erasure[edit]

Generics are checked at compile-time for type-correctness. The generic type information is then removed in a process called type erasure. For example, List<Integer> will be converted to the non-generic type List, which ordinarily contains arbitrary objects. The compile-time check guarantees that the resulting code is type-correct.

Consequent to type erasure, type parameters cannot be determined at run-time. For example, when an ArrayList is examined at runtime, there is no general way to determine whether, before type erasure, it was an ArrayList<Integer> or an ArrayList<Float>. Many people are dissatisfied with this restriction.[5] There are partial approaches. For example, individual elements may be examined to determine the type they belong to; for example, if an ArrayList contains an Integer, that ArrayList may have been parameterized with Integer (however, it may have been parameterized with any parent of Integer, such as Number or Object).

Demonstrating this point, the following code outputs "Equal":

ArrayList<Integer> li = new ArrayList<Integer>();
ArrayList<Float> lf = new ArrayList<Float>();
if (li.getClass() == lf.getClass()) // evaluates to true
  System.out.println("Equal");

Another effect of type erasure is that a generic class cannot extend the Throwable class in any way, directly or indirectly:[6]

public class GenericException<T> extends Exception

The reason why this is not supported is due to type erasure:

try
{
	throw GenericException();
}
catch(GenericException<Integer> e)
{
	//ignore
}
catch(GenericException<String> e)
{
	e.printStackTrace();
}

Due to type erasure, the runtime will not know which catch block to execute, so this is prohibited by the compiler.

Java generics differ from C++ templates. Java generics generate only one compiled version of a generic class or function regardless of the number of parameterizing types used. Furthermore, the Java run-time environment does not need to know which parameterized type is used because the type information is validated at compile-time and is not included in the compiled code. Consequently, instantiating a Java class of a parameterized type is impossible because instantiation requires a call to a constructor, which is unavailable if the type is unknown.

For example, the following code will not compile:

<T> T instantiateElementType(List<T> arg) {
  return new T(); //causes a compile error
}

Because there is only one copy per generic class at runtime, static variables are shared among all the instances of the class, regardless of their type parameter. Consequently the type parameter cannot be used in the declaration of static variables or in static methods.

See also[edit]

References[edit]

  1. ^ Java Programming Language
  2. ^ GJ: Generic Java
  3. ^ Java Language Specification, Third Edition by James Gosling, Bill Joy, Guy Steele, Gilad Bracha – Prentice Hall PTR 2005
  4. ^ http://www.oracle.com/technetwork/java/javase/generics-tutorial-159168.pdf – Generics in the Java Programming Language
  5. ^ Gafter, Neal (2006-11-05). "Reified Generics for Java". Retrieved 2010-04-20. 
  6. ^ "Java Language Specification, Section 8.1.2". Oracle. Retrieved 21 March 2013. 

External links[edit]