Generics in JavaGenerics 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".[1] 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.[2][3] 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.[4] Generic Java was incorporated in Java with the addition of wildcards. Hierarchy and classificationAccording to Java Language Specification:[5]
MotivationThe following block of Java code illustrates a problem that exists when not using generics. First, it declares an 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
Although the code is compiled without error, it throws a runtime exception ( The above code fragment can be rewritten using generics as follows: final List<String> v = new ArrayList<String>();
v.add("test");
final Integer i = (Integer) v.get(0); // (type error) compilation-time error
The type parameter 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 Here is a small excerpt from the definition of the interfaces interface List<E> {
void add(E x);
Iterator<E> iterator();
}
interface Iterator<E> {
E next();
boolean hasNext();
}
Generic class definitionsHere is an example of a generic Java class, which can be used to represent individual entries (key to value mappings) in a map: 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 "(" + key + ", " + value + ")";
}
}
This generic class could be used in the following ways, for example: final Entry<String, String> grade = new Entry<String, String>("Mike", "A");
final Entry<String, Integer> mark = new Entry<String, Integer>("Mike", 100);
System.out.println("grade: " + grade);
System.out.println("mark: " + mark);
final Entry<Integer, Boolean> prime = new Entry<Integer, Boolean>(13, true);
if (prime.getValue()) {
System.out.println(prime.getKey() + " is prime.");
}
else {
System.out.println(prime.getKey() + " is not prime.");
}
It outputs: grade: (Mike, A) mark: (Mike, 100) 13 is prime. Generic method definitionsHere is an example of a generic method using the generic class above: public static <Type> Entry<Type, Type> twice(Type value) {
return new Entry<Type, Type>(value, value);
}
Note: If we remove the first In many cases, the user of the method need not indicate the type parameters, as they can be inferred: final Entry<String, String> pair = Entry.twice("Hello");
The parameters can be explicitly added if needed: final Entry<String, String> pair = Entry.<String>twice("Hello");
The use of primitive types is not allowed, and boxed versions must be used instead: final Entry<int, int> pair; // Fails compilation. Use Integer instead.
There is also the possibility to create generic methods based on given parameters. public <Type> Type[] toArray(Type... elements) {
return elements;
}
In such cases you can't use primitive types either, e.g.: Integer[] array = toArray(1, 2, 3, 4, 5, 6);
Diamond operatorThanks to type inference, Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets ( final Entry<String, String> grade = new Entry<>("Mike", "A");
final Entry<String, Integer> mark = new Entry<>("Mike", 100);
System.out.println("grade: " + grade);
System.out.println("mark: " + mark);
final Entry<Integer, Boolean> prime = new Entry<>(13, true);
if (prime.getValue()) System.out.println(prime.getKey() + " is prime.");
else System.out.println(prime.getKey() + " is not prime.");
Type wildcardsA 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 " Here is an example where the element type of a final Collection<?> c = new ArrayList<String>();
c.add(new Object()); // compile-time error
c.add(null); // allowed
Since we don't know what the element type of To specify the upper bound of a type wildcard, the The use of wildcards above adds flexibility[12] since there is not any inheritance relationship between any two parameterized types with concrete type as type argument. Neither final List<Integer> ints = new ArrayList<>();
ints.add(2);
final List<Number> nums = ints; // valid if List<Integer> were a subtype of List<Number> according to substitution rule.
nums.add(3.14);
final 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: final List<? extends Number> nums = ints; // OK
nums.add(3.14); // compile-time error
nums.add(null); // allowed
To specify the lower bounding class of a type wildcard, the 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.[12] Generics in throws clauseAlthough 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 erasureGenerics are checked at compile-time for type-correctness.[7] The generic type information is then removed in a process called type erasure.[6] For example, Because of type erasure, type parameters cannot be determined at run-time.[6] For example, when an Demonstrating this point, the following code outputs "Equal": final List<Integer> li = new ArrayList<>();
final List<Float> lf = new ArrayList<>();
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 public class GenericException<T> extends Exception
The reason why this is not supported is due to type erasure: try {
throw new GenericException<Integer>();
}
catch (GenericException<Integer> e) {
System.err.println("Integer");
}
catch (GenericException<String> e) {
System.err.println("String");
}
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: <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. Type erasure was implemented in Java to maintain backward compatibility with programs written prior to Java SE5.[7] Differences from ArraysThere are several important differences between arrays (both primitive arrays and Covariance, contravariance and invarianceGenerics are invariant, whereas arrays are covariant.[6] This is a benefit of using generic when compared to non-generic objects such as arrays.[6] 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 If the developer declares a ReificationArrays are reified, meaning that an array object enforces its type information at run-time, whereas generics in Java are not reified.[6] More formally speaking, objects with generic type in Java are non-reifiable types.[6] A non-reifiable type is type whose representation at run-time has less information than its representation at compile-time.[6] Objects with generic type in Java are non-reifiable due to type erasure.[6] 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.[6] Examples of non-reifiable types include Project on genericsProject Valhalla is an experimental project to incubate improved Java generics and language features, for future versions potentially from Java 10 onwards. Potential enhancements include:[16]
See also
Citations
References
|
Portal di Ensiklopedia Dunia