GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — Generics in Java - Wikipedia 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".[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 import java.util.ArrayList; import java.util.List; List v = new ArrayList(); v.add("test"); // A String that cannot be cast to an Integer Integer i = (Integer)v.get(0); // Run time error This is similar to C, which lacks generics, and its collection-like data structures typically store data as Although the code is compiled without error, it throws a runtime exception ( The above code fragment can be rewritten using generics as follows: import java.util.ArrayList; import java.util.List; List<String> v = new ArrayList<String>(); v.add("test"); 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<K, V> { private K key; private V value; public Entry(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } public String toString() { return String.format("(%s, %s)", key, value); } } This generic class could be used in the following ways, for example: Entry<String, String> grade = new Entry<String, String>("Mike", "A"); Entry<String, Integer> mark = new Entry<String, Integer>("Mike", 100); System.out.printf("grade: %s%n", grade); System.out.printf("mark: %s%n", mark); 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()); } 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, for a generic type public static <T> Entry<T, T> twice(T value) { return new Entry<T, T>(value, value); } In many cases, the user of the method need not indicate the type parameters, as they can be inferred: Entry<String, String> pair = Entry.twice("Hello"); The parameters can be explicitly added if needed: Entry<String, String> pair = Entry.<String>twice("Hello"); The use of primitive types is not allowed, and boxed versions must be used instead; only reference types may be stored in a generic type parameter. For instance, There is also the possibility to create generic methods based on given parameters. public <T> T[] toArray(T... elements) { return elements; } In such cases one cannot use primitive types either, e.g.: Integer[] array = toArray(1, 2, 3, 4, 5, 6); While primitives cannot be stored, arrays of primitives (as they are objects, not primitives themselves) can: 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; Diamond operatorUsing type inference, Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets ( Thus, the above code example using Entry<String, String> grade = new Entry<>("Mike", "A"); Entry<String, Integer> mark = new Entry<>("Mike", 100); System.out.printf("grade: %s%n", grade); System.out.printf("mark: %s%n", mark); 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()); } The so-called "diamond operator" does not exist in other Java-derived languages like C# or Kotlin, which have their own ways to avoid redundant type information. While something visually similar exists in C++, it does not denote type parameter deduction, but rather refers to only the default type parameter. template <typename T = int> class Box { // ... }; Box<> v; // refers to Box<int> Type wildcardsMain article: 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 " Here is an example where the element type of a import java.util.ArrayList; import java.util.Collection; Collection<?> c = new ArrayList<String>(); c.add(new Object()); // compile-time error c.add(null); // allowed Since the element type of To specify the upper bound of a type wildcard, the The use of wildcards above adds flexibility[13] since there is not any inheritance relationship between any two parameterized types with concrete type as type argument. Neither import java.util.ArrayList; import java.util.List; List<Integer> intList = new ArrayList<>(); intList.add(2); List<Number> nums = intList; // valid if List<Integer> were a subtype of List<Number> according to substitution rule. nums.add(3.14); Integer x = intList.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: import java.util.List; 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.[13] Generics in throws clauseAlthough exceptions themselves cannot be generic, type parameters can appear in a throws clause: public <T extends Throwable> void throwMeConditional(boolean conditional, T exception) throws T { if (conditional) { throw exception; } } In this case, it is possible as the type parameter is bounded below by The following pattern, sometimes known as the generic rethrow or sneaky throw, is used to bypass checked exceptions entirely, such as in the Project Lombok static <T extends Throwable> void sneakyThrow(Throwable t) throws T { throw (T) t; } Problems with type erasureGenerics are checked at compile-time for type-correctness.[8] The generic type information is then removed in a process called type erasure.[7] For example, Because of type erasure, type parameters cannot be determined at run-time.[7] For example, when an Demonstrating this point, the following code outputs "Equal": import java.util.ArrayList; import java.util.List; List<Integer> li = new ArrayList<>(); 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 // causes a compile error 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: import java.util.List; <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.[8] Differences from ArraysThere are several important differences between arrays (both primitive arrays and Covariance, contravariance and invarianceMain article: Covariance and contravariance (computer science)
Generics are invariant, whereas arrays are covariant.[7] This is a benefit of using generic when compared to non-generic objects such as arrays.[7] 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 ReificationMain 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.[7] This is because arrays are real compiler-generated classes in the JVM; for each type More formally speaking, objects with generic type in Java are non-reifiable types.[7] A non-reifiable type is type whose representation at run-time has less information than its representation at compile-time.[7] Objects with generic type in Java are non-reifiable due to type erasure.[7] 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.[7] 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:[18]
See alsoCitations
References
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |