{{Short description|Features of the Java programming language}} '''Generics''' are a facility of generic programming that were added to the Java programming language in 2004 within version J2SE 5.0. They were designed to extend Java's type system to allow "a type or method to operate on objects of various types while providing compile-time type safety".<ref>[http://java.sun.com/j2se/1.5.0/docs/guide/language/index.html Java Programming Language]</ref> The aspect ''compile-time type safety'' required that parametrically polymorphic functions are not implemented in the Java virtual machine, since type safety is impossible in this case.<ref>A ClassCastException can be thrown even in the absence of casts or nulls.{{cite web|url=https://raw.githubusercontent.com/namin/unsound/master/doc/unsound-oopsla16.pdf|title=Java and Scala's Type Systems are Unsound}}</ref>{{sfn|Bloch|2018|loc=Chapter §5 Item 27: Eliminate unchecked warnings|pp=123-125}}
The Java collections framework supports generics to specify the type of objects stored in a collection instance.
In 1998, Gilad Bracha, Martin Odersky, David Stoutamire and Philip Wadler created Generic Java, an extension to the Java language to support generic types.<ref>[http://homepages.inf.ed.ac.uk/wadler/gj/ GJ: Generic Java]</ref> Generic Java was incorporated in Java with the addition of wildcards.
==Hierarchy and classification== According to ''Java Language Specification'':<ref>[http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html Java Language Specification, Third Edition] by James Gosling, Bill Joy, Guy Steele, Gilad Bracha – Prentice Hall PTR 2005</ref> *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.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} It defines one or more type variables that act as parameters.{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}} 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.{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}} It defines one or more type variables that act as parameters.{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}} 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.{{sfn|Bloch|2018|loc=Chapter §5 Item 30: Favor generic methods|pp=135-138}} 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== The following block of Java code illustrates a problem that exists when not using generics. First, it declares an '''{{Javadoc:SE|name=ArrayList|package=java.util|java/util|ArrayList}}''' of type '''{{Javadoc:SE|name=Object|package=java.lang|java/lang|Object}}'''. Then, it adds a <code>String</code> to the <code>ArrayList</code>. Finally, it attempts to retrieve the added <code>String</code> and cast it to an <code>Integer</code>—an error in logic, as it is impossible to cast any string instance to an integer.
<syntaxhighlight lang="Java"> import java.util.ArrayList; import java.util.List;
final List v = new ArrayList(); v.add("test"); // A String that cannot be cast to an Integer final Integer i = (Integer)v.get(0); // Run time error </syntaxhighlight>
This is similar to C, which lacks generics. Collections typically store as <code>void*</code> (a void pointer), from which objects must be cast explicitly.
Although the code is compiled without error, it throws a runtime exception (<code>java.lang.ClassCastException</code>) when executing the third line of code. This type of logic error can be detected during compile time by using generics{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}} and is the primary motivation for using them.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} It defines one or more type variables that act as parameters.
The above code fragment can be rewritten using generics as follows:
<syntaxhighlight lang="Java"> import java.util.ArrayList; import java.util.List;
final List<String> v = new ArrayList<String>(); v.add("test"); final Integer i = (Integer)v.get(0); // (type error) compilation-time error </syntaxhighlight>
The type parameter <code>String</code> within the angle brackets declares the <code>ArrayList</code> to be constituted of <code>String</code> (a descendant of the <code>ArrayList</code>'s generic <code>Object</code> constituents). With generics, it is no longer necessary to cast the third line to any particular type, because the result of <code>v.get(0)</code> is defined as <code>String</code> by the code generated by the compiler.
The logical flaw in the third line of this fragment will be detected as a compile-time error (with J2SE 5.0 or later) because the compiler will detect that <code>v.get(0)</code> returns <code>String</code> instead of <code>Integer</code>.{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}} For a more elaborate example, see reference.<ref>{{cite web |url=http://www.oracle.com/technetwork/java/javase/generics-tutorial-159168.pdf |title=Generics in the Java Programming Language |author=Gilad Bracha |date=July 5, 2004 |website=www.oracle.com}}</ref>
Here is a small excerpt from the definition of the interfaces '''{{Javadoc:SE|package=java.util|java/util|List}}''' and '''{{Javadoc:SE|package=java.util|java/util|Iterator}}''' in package '''{{Javadoc:SE|package=java.util|java/util}}''':
<syntaxhighlight lang="Java"> interface List<E> { void add(E x); Iterator<E> iterator(); }
interface Iterator<E> { E next(); boolean hasNext(); } </syntaxhighlight>
==Generic class definitions== Here is an example of a generic Java class, which can be used to represent individual entries (key to value mappings) in a map:
<syntaxhighlight lang="Java"> public class Entry<KeyType, ValueType> { private final KeyType key; private final ValueType value;
public Entry(KeyType key, ValueType value) { this.key = key; this.value = value; }
public KeyType getKey() { return key; }
public ValueType getValue() { return value; }
public String toString() { return String.format("(%s, %s)", key, value); } } </syntaxhighlight>
This generic class could be used in the following ways, for example:
<syntaxhighlight lang="Java"> final Entry<String, String> grade = new Entry<String, String>("Mike", "A"); final Entry<String, Integer> mark = new Entry<String, Integer>("Mike", 100); System.out.printf("grade: %s%n", grade); System.out.printf("mark: %s%n", mark);
final Entry<Integer, Boolean> prime = new Entry<>(13, true); if (prime.getValue()) { System.out.println("%s is prime.%n", prime.getKey()); } else { System.out.println("%s is not prime.%n", prime.getKey()); } </syntaxhighlight>
It outputs: <pre> grade: (Mike, A) mark: (Mike, 100) 13 is prime. </pre>
==Generic method definitions== Here is an example of a generic method using the generic class above, for a generic type <code>T</code>: <syntaxhighlight lang="Java"> public static <T> Entry<T, T> twice(T value) { return new Entry<T, T>(value, value); } </syntaxhighlight>
Note: If we remove the first <code><T></code> in the above method, we will get compilation error (cannot find symbol "<code>T</code>"), 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:
<syntaxhighlight lang="Java"> final Entry<String, String> pair = Entry.twice("Hello"); </syntaxhighlight>
The parameters can be explicitly added if needed:
<syntaxhighlight lang="Java"> final Entry<String, String> pair = Entry.<String>twice("Hello"); </syntaxhighlight>
The use of primitive types is not allowed, and boxed versions must be used instead:
<syntaxhighlight lang="Java"> final Entry<int, int> pair; // Fails compilation. Use Integer instead. </syntaxhighlight>
There is also the possibility to create generic methods based on given parameters.
<syntaxhighlight lang="Java"> public <T> T[] toArray(T... elements) { return elements; } </syntaxhighlight>
In such cases one cannot use primitive types either, e.g.:
<syntaxhighlight lang="Java"> Integer[] array = toArray(1, 2, 3, 4, 5, 6); </syntaxhighlight>
While primitives cannot be stored, arrays of primitives (as they are objects, not primitives themselves) can: <syntaxhighlight lang="Java"> import java.util.ArrayList; import java.util.List;
// List<int[]> and List<Integer[]> are equally acceptable List<int[]> listOfArrays = new ArrayList<>(); List<Integer[]> myOtherList = listOfArrays; </syntaxhighlight>
==Diamond operator== Using type inference, Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets (<code><></code>, called the ''diamond operator'') for a pair of angle brackets containing the one or more type parameters that a sufficiently close context implies.<ref>{{cite web | url=http://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html | title=Type Inference for Generic Instance Creation }}</ref> While <code><></code> is often called the "diamond operator", it is not an operator, just an empty type parameter list.
Thus, the above code example using <code>Entry</code> can be rewritten as:
<syntaxhighlight lang="Java"> final Entry<String, String> grade = new Entry<>("Mike", "A"); final Entry<String, Integer> mark = new Entry<>("Mike", 100);
System.out.printf("grade: %s%n", grade); System.out.printf("mark: %s%n", mark);
final Entry<Integer, Boolean> prime = new Entry<>(13, true); if (prime.getValue()) { System.out.println("%s is prime.%n", prime.getKey()); } else { System.out.println("%s is not prime.%n", prime.getKey()); } </syntaxhighlight>
The so-called diamond operator does not exist in other Java-derived languages like C# or Kotlin, which have their own ways of avoiding redundant type information. While something visually similar does exist in C++, it does not denote type parameter deduction, but rather just refers to the default type parameter. <syntaxhighlight lang="cpp"> template <typename T = int> class MyVector { // ... };
MyVector<> v; // refers to MyVector<int> </syntaxhighlight>
==Type wildcards== {{main|Wildcard (Java)}} 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 "<code><?></code>"; optionally 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 an object that uses parameterized types.
Here is an example where the element type of a <code>Collection<E></code> is parameterized by a wildcard:
<syntaxhighlight lang="java"> import java.util.ArrayList; import java.util.Collection;
final Collection<?> c = new ArrayList<String>(); c.add(new Object()); // compile-time error c.add(null); // allowed </syntaxhighlight>
Since we don't know what the element type of <code>c</code> stands for, we cannot add objects to it. The <code>add()</code> method takes arguments of type <code>E</code>, the element type of the <code>Collection<E></code> generic interface. When the actual type argument is <code>?</code>, it stands for some unknown type. Any method argument value we pass to the <code>add()</code> method would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null; which is a member of every type.<ref>{{cite web |url=http://www.oracle.com/technetwork/java/javase/generics-tutorial-159168.pdf |title=Generics in the Java Programming Language |author=Gilad Bracha |date=July 5, 2004 |pages=5 |website=www.oracle.com}}</ref>
To specify the upper bound of a type wildcard, the {{java|extends}} keyword is used to indicate that the type argument is a subtype of the bounding class.{{sfn|Bloch|2018|loc=Chapter §5 Item 31: Use bounded wildcards to increase API flexibility|pp=139-145}} So {{java|List<? extends Number>}} means that the given list contains objects of some unknown type which extends the <code>Number</code> class. For example, the list could be <code>List<Float></code> or <code>List<Number></code>. Reading an element from the list will return a <code>Number</code>. Adding null elements is, again, also allowed.<ref>{{Cite web |url=https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html |title=Wildcards > Bonus > Generics |last=Bracha |first=Gilad |author-link=Gilad Bracha |publisher=Oracle |website=The Java™ Tutorials |quote=...The sole exception is null, which is a member of every type...}}</ref>
The use of wildcards above adds flexibility{{sfn|Bloch|2018|loc=Chapter §5 Item 31: Use bounded wildcards to increase API flexibility|pp=139-145}} since there is not any inheritance relationship between any two parameterized types with concrete type as type argument. Neither <code>List<Number></code> nor <code>List<Integer></code> is a subtype of the other; even though <code>Integer</code> is a subtype of <code>Number</code>.{{sfn|Bloch|2018|loc=Chapter §5 Item 31: Use bounded wildcards to increase API flexibility|pp=139-145}} So, any method that takes <code>List<Number></code> as a parameter does not accept an argument of <code>List<Integer></code>. If it did, it would be possible to insert a <code>Number</code> that is not an <code>Integer</code> into it; which violates type safety. Here is an example that demonstrates how type safety would be violated if <code>List<Integer></code> were a subtype of <code>List<Number></code>:
<syntaxhighlight lang="java"> import java.util.ArrayList; import java.util.List;
final List<Integer> intList = new ArrayList<>(); intList.add(2); final List<Number> nums = intList; // valid if List<Integer> were a subtype of List<Number> according to substitution rule. nums.add(3.14); final Integer x = intList.get(1); // now 3.14 is assigned to an Integer variable! </syntaxhighlight>
The solution with wildcards works because it disallows operations that would violate type safety:
<syntaxhighlight lang="java"> import java.util.List;
final List<? extends Number> nums = ints; // OK nums.add(3.14); // compile-time error nums.add(null); // allowed </syntaxhighlight>
To specify the lower bounding class of a type wildcard, the <code>super</code> keyword is used. This keyword indicates that the type argument is a supertype of the bounding class. So, {{java|List<? super Number>}} could represent <code>List<Number></code> or <code>List<Object></code>. Reading from a list defined as {{java|List<? super Number>}} returns elements of type <code>Object</code>. Adding to such a list requires either elements of type <code>Number</code>, any subtype of <code>Number</code> or null (which is a member of every type).
The mnemonic 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.{{sfn|Bloch|2018|loc=Chapter §5 Item 31: Use bounded wildcards to increase API flexibility|pp=139-145}}
==Generics in throws clause== Although exceptions themselves cannot be generic, generic parameters can appear in a throws clause:
<syntaxhighlight lang="Java"> public <T extends Throwable> void throwMeConditional(boolean conditional, T exception) throws T { if (conditional) { throw exception; } } </syntaxhighlight>
==Problems with type erasure== Generics are checked at compile-time for type-correctness.{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}} The generic type information is then removed in a process called type erasure.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} For example, <code>List<Integer></code> will be converted to the non-generic type <code>List</code>, which ordinarily contains arbitrary objects. The compile-time check guarantees that the resulting code uses the correct type.{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}}
Because of type erasure, type parameters cannot be determined at run-time.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} For example, when an <code>ArrayList</code> is examined at runtime, there is no general way to determine whether, before type erasure, it was an <code>ArrayList<Integer></code> or an <code>ArrayList<Float></code>. Many people are dissatisfied with this restriction.<ref>{{cite web|first=Neal|last=Gafter|title=Reified Generics for Java|date=2006-11-05|access-date=2010-04-20|url=http://gafter.blogspot.ro/2006/11/reified-generics-for-java.html}}</ref> There are partial approaches. For example, individual elements may be examined to determine the type they belong to; for example, if an <code>ArrayList</code> contains an <code>Integer</code>, that ArrayList may have been parameterized with <code>Integer</code> (however, it may have been parameterized with any parent of <code>Integer</code>, such as <code>Number</code> or <code>Object</code>).
Demonstrating this point, the following code outputs "Equal": <syntaxhighlight lang="java"> import java.util.ArrayList; import java.util.List;
final List<Integer> li = new ArrayList<>(); final List<Float> lf = new ArrayList<>(); if (li.getClass() == lf.getClass()) { // evaluates to true System.out.println("Equal"); } </syntaxhighlight>
Another effect of type erasure is that a generic class cannot extend the <code>Throwable</code> class in any way, directly or indirectly:<ref>{{cite web|url=http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.2|title=Java Language Specification, Section 8.1.2|publisher=Oracle|access-date=24 October 2015}}</ref>
<syntaxhighlight lang="java"> // causes a compile error public class GenericException<T> extends Exception { // ... } </syntaxhighlight> The reason why this is not supported is due to type erasure: <syntaxhighlight lang="java"> try { throw new GenericException<Integer>(); } catch (GenericException<Integer> e) { System.err.println("Integer"); } catch (GenericException<String> e) { System.err.println("String"); } </syntaxhighlight> 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 cannot be compiled: <syntaxhighlight lang="java"> import java.util.List;
<T> T instantiateElementType(List<T> arg) { return new T(); // causes a compile error } </syntaxhighlight>
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.
Type erasure was implemented in Java to maintain backward compatibility with programs written prior to Java SE5.{{sfn|Bloch|2018|loc=Chapter §5 Item 26: Don't use raw types|pp=117-122}}
== Differences from Arrays ==
There are several important differences between arrays (both primitive arrays and {{code|Object}} arrays), and generics in Java. Two of the major differences, namely, differences in terms of variance and reification.
=== Covariance, contravariance and invariance ===
{{main article|Covariance and contravariance (computer science)}} Generics are invariant, whereas arrays are covariant.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} This is a benefit of using generic when compared to non-generic objects such as arrays.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} Specifically, generics can help prevent run time exceptions by throwing a compile-time exception to force the developer to fix the code.
For example, if a developer declares an {{code|Object[]}} object and instantiates the object as a new {{code|Long[]}} object, no compile-time exception is thrown (since arrays are covariant).{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} This may give the false impression that the code is correctly written. However, if the developer attempts to add a {{code|String}} to this {{code|Long[]}} object, the program will throw an {{code|ArrayStoreException}}.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} This run-time exception can be completely avoided if the developer uses generics.
If the developer declares a {{code|Collection<Object>}} object an creates a new instance of this object with return type {{code|ArrayList<Long>}}, the Java compiler will (correctly) throw a compile-time exception to indicate the presence of incompatible types (since generics are invariant).{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} Hence, this avoids potential run-time exceptions. This problem can be fixed by creating an instance of {{code|Collection<Object>}} using {{code|ArrayList<Object>}} object instead. For code using Java SE7 or later versions, the {{code|Collection<Object>}} can be instantiated with an {{code|ArrayList<>}} object using the diamond operator
=== Reification === {{main article|Reification (computer science)}} Arrays are reified, meaning that an array object enforces its type information at run-time, whereas generics in Java are not reified.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}}
More formally speaking, objects with generic type in Java are non-reifiable types.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} A non-reifiable type is type whose representation at run-time has less information than its representation at compile-time.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}}
Objects with generic type in Java are non-reifiable due to type erasure.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} Java only enforces type information at compile-time. After the type information is verified at compile-time, the type information is discarded, and at run-time, the type information will not be available.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}}
Examples of non-reifiable types include {{code|List<T>}} and {{code|List<String>}}, where {{code|T}} is a generic formal parameter. {{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}}
==Project on generics==
Project Valhalla is an experimental project to incubate improved Java generics and language features, for future versions potentially from Java 10 onwards. Potential enhancements include:<ref>{{cite web|last1=Goetz|first1=Brian|title=Welcome to Valhalla!|url=http://mail.openjdk.java.net/pipermail/valhalla-dev/2014-July/000000.html|website=OpenJDK mail archive|publisher=OpenJDK|access-date=12 August 2014}}</ref> * generic specialization, e.g. <code>List<int></code> rather than <code>List<Integer></code> * reified generics; making actual types available at runtime.
==See also== * Generic programming * Template metaprogramming * Wildcard (Java) * Comparison of C# and Java * Comparison of Java and C++
==Citations== {{reflist}}
==References== *{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}
<!--======================== {{No more links}} ============================ | PLEASE BE CAUTIOUS IN ADDING MORE LINKS TO THIS ARTICLE. Wikipedia | | is not a collection of links nor should it be used for advertising. | | | | Excessive or inappropriate links WILL BE DELETED. | | See Wikipedia:External links & Wikipedia:Spam for details. | | | | If there are already plentiful links, please propose additions or | | replacements on this article's discussion page, or submit your link | | to the relevant category at the Open Directory Project (dmoz.org) | | and link back to that category using the {{dmoz}} template. | ======================= {{No more links}} =============================-->
{{DEFAULTSORT:Generics In Java}} Category:Java (programming language) Category:Polymorphism (computer science)