fbpx
Wikipedia

Generics in Java

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 was not fully achieved, since it was shown in 2016 that it is not guaranteed in all cases.[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 classification edit

According to Java Language Specification:[5]

  • 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.[6] It defines one or more type variables that act as parameters.[7] 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.[7] It defines one or more type variables that act as parameters.[7] 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.[8] 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—an error in logic, as it is not generally possible to cast an arbitrary string to an integer.

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 (java.lang.ClassCastException) when executing the third line of code. This type of logic error can be detected during compile time by using generics[7] and is the primary motivation for using them.[6] It defines one or more type variables that act as parameters.

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 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.

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 v.get(0) returns String instead of Integer.[7] For a more elaborate example, see reference.[9]

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

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

Generic class definitions edit

Here 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 definitions edit

Here 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 <Type> in the above method, we will get compilation error (cannot find symbol "Type"), 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:

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 operator edit

Thanks to type inference, Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets (<>, called the diamond operator) for a pair of angle brackets containing the one or more type parameters that a sufficiently close context implies.[10] Thus, the above code example using Entry can be rewritten as:

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 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 "<?>"; 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 Collection<E> is parameterized by a wildcard:

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 c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the Collection<E> generic interface. When the actual type argument is ?, it stands for some unknown type. Any method argument value we pass to the add() 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.[11]

To specify the upper bound of a type wildcard, the extends keyword is used to indicate that the type argument is a subtype of the bounding class.[12] 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. Adding null elements is, again, also allowed.[13]

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 List<Number> nor List<Integer> is a subtype of the other; even though Integer is a subtype of Number.[12] 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 an example that demonstrates how type safety would be violated if List<Integer> were a subtype of List<Number>:

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 super keyword is used. This keyword indicates that the type argument is a supertype of the bounding class. 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 either elements of type Number, any subtype of Number 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.[12]

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.[7] The generic type information is then removed in a process called type erasure.[6] 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 uses the correct type.[7]

Because of type erasure, type parameters cannot be determined at run-time.[6] 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.[14] 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":

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 Throwable class in any way, directly or indirectly:[15]

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 Arrays edit

There are several importance differences between arrays (both primitive arrays and Object arrays), and generics in Java. Two of the major differences, namely, differences in terms of variance and reification.

Covariance, contravariance and invariance edit

Generics 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 Object[] object and instantiates the object as a new Long[] object, no compile-time exception is thrown (since arrays are covariant).[6] This may give the false impression that the code is correctly written. However, if the developer attempts to add a String to this Long[] object, the program will throw an ArrayStoreException.[6] This run-time exception can be completely avoided if the developer uses generics.

If the developer declares a Collection<Object> object an creates a new instance of this object with return type ArrayList<Long>, the Java compiler will (correctly) throw a compile-time exception to indicate the presence of incompatible types (since generics are invariant).[6] Hence, this avoids potential run-time exceptions. This problem can be fixed by creating an instance of Collection<Object> using ArrayList<Object> object instead. For code using Java SE7 or later versions, the Collection<Object> can be instantiated with an ArrayList<> object using the diamond operator

Reification edit

Arrays 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 List<T> and List<String>, where T is a generic formal parameter. [6]

Project on generics edit

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:[16]

See also edit

Citations edit

  1. ^ Java Programming Language
  2. ^ A ClassCastException can be thrown even in the absence of casts or nulls."Java and Scala's Type Systems are Unsound" (PDF).
  3. ^ Bloch 2018, pp. 123–125, Chapter §5 Item 27: Eliminate unchecked warnings.
  4. ^ GJ: Generic Java
  5. ^ Java Language Specification, Third Edition by James Gosling, Bill Joy, Guy Steele, Gilad Bracha – Prentice Hall PTR 2005
  6. ^ a b c d e f g h i j k l m n o Bloch 2018, pp. 126–129, Chapter §5 Item 28: Prefer lists to arrays.
  7. ^ a b c d e f g h Bloch 2018, pp. 117–122, Chapter §5 Item 26: Don't use raw types.
  8. ^ Bloch 2018, pp. 135–138, Chapter §5 Item 30: Favor generic methods.
  9. ^ Gilad Bracha (July 5, 2004). "Generics in the Java Programming Language" (PDF). www.oracle.com.
  10. ^ "Type Inference for Generic Instance Creation".
  11. ^ Gilad Bracha (July 5, 2004). "Generics in the Java Programming Language" (PDF). www.oracle.com. p. 5.
  12. ^ a b c d Bloch 2018, pp. 139–145, Chapter §5 Item 31: Use bounded wildcards to increase API flexibility.
  13. ^ Bracha, Gilad. "Wildcards > Bonus > Generics". The Java™ Tutorials. Oracle. ...The sole exception is null, which is a member of every type...
  14. ^ Gafter, Neal (2006-11-05). "Reified Generics for Java". Retrieved 2010-04-20.
  15. ^ "Java Language Specification, Section 8.1.2". Oracle. Retrieved 24 October 2015.
  16. ^ Goetz, Brian. "Welcome to Valhalla!". OpenJDK mail archive. OpenJDK. Retrieved 12 August 2014.

References edit

  • Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.

generics, java, generics, facility, generic, programming, that, were, added, java, programming, language, 2004, within, version, j2se, they, were, designed, extend, java, type, system, allow, type, method, operate, objects, various, types, while, providing, co. 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 was not fully achieved since it was shown in 2016 that it is not guaranteed in all cases 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 Contents 1 Hierarchy and classification 2 Motivation 3 Generic class definitions 4 Generic method definitions 5 Diamond operator 6 Type wildcards 7 Generics in throws clause 8 Problems with type erasure 9 Differences from Arrays 9 1 Covariance contravariance and invariance 9 2 Reification 10 Project on generics 11 See also 12 Citations 13 ReferencesHierarchy and classification editAccording to Java Language Specification 5 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 6 It defines one or more type variables that act as parameters 7 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 7 It defines one or more type variables that act as parameters 7 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 8 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 editThe 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 an error in logic as it is not generally possible to cast an arbitrary string to an integer 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 java lang ClassCastException when executing the third line of code This type of logic error can be detected during compile time by using generics 7 and is the primary motivation for using them 6 It defines one or more type variables that act as parameters The above code fragment can be rewritten using generics as follows final List lt String gt v new ArrayList lt String gt v add test final Integer i Integer v get 0 type error compilation 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 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 v get 0 returns String instead of Integer 7 For a more elaborate example see reference 9 Here is a small excerpt from the definition of the interfaces java util List and java util Iterator in package java util interface List lt E gt void add E x Iterator lt E gt iterator interface Iterator lt E gt E next boolean hasNext Generic class definitions editHere 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 lt KeyType ValueType gt 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 lt String String gt grade new Entry lt String String gt Mike A final Entry lt String Integer gt mark new Entry lt String Integer gt Mike 100 System out println grade grade System out println mark mark final Entry lt Integer Boolean gt prime new Entry lt Integer Boolean gt 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 definitions editHere is an example of a generic method using the generic class above public static lt Type gt Entry lt Type Type gt twice Type value return new Entry lt Type Type gt value value Note If we remove the first lt Type gt in the above method we will get compilation error cannot find symbol Type 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 final Entry lt String String gt pair Entry twice Hello The parameters can be explicitly added if needed final Entry lt String String gt pair Entry lt String gt twice Hello The use of primitive types is not allowed and boxed versions must be used instead final Entry lt int int gt pair Fails compilation Use Integer instead There is also the possibility to create generic methods based on given parameters public lt Type gt 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 operator editThanks to type inference Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets lt gt called the diamond operator for a pair of angle brackets containing the one or more type parameters that a sufficiently close context implies 10 Thus the above code example using Entry can be rewritten as final Entry lt String String gt grade new Entry lt gt Mike A final Entry lt String Integer gt mark new Entry lt gt Mike 100 System out println grade grade System out println mark mark final Entry lt Integer Boolean gt prime new Entry lt gt 13 true if prime getValue System out println prime getKey is prime else System out println prime getKey is not prime Type wildcards editMain 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 lt gt 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 Collection lt E gt is parameterized by a wildcard final Collection lt gt c new ArrayList lt String gt c add new Object compile time error c add null allowed Since we don t know what the element type of c stands for we cannot add objects to it The add method takes arguments of type E the element type of the Collection lt E gt generic interface When the actual type argument is it stands for some unknown type Any method argument value we pass to the add 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 11 To specify the upper bound of a type wildcard the span class kd extends span keyword is used to indicate that the type argument is a subtype of the bounding class 12 So span class n List span span class o lt span span class w span span class kd extends span span class w span span class n Number span span class o gt span means that the given list contains objects of some unknown type which extends the Number class For example the list could be List lt Float gt or List lt Number gt Reading an element from the list will return a Number Adding null elements is again also allowed 13 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 List lt Number gt nor List lt Integer gt is a subtype of the other even though Integer is a subtype of Number 12 So any method that takes List lt Number gt as a parameter does not accept an argument of List lt Integer gt If it did it would be possible to insert a Number that is not an Integer into it which violates type safety Here is an example that demonstrates how type safety would be violated if List lt Integer gt were a subtype of List lt Number gt final List lt Integer gt ints new ArrayList lt gt ints add 2 final List lt Number gt nums ints valid if List lt Integer gt were a subtype of List lt Number gt 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 lt extends Number gt 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 super keyword is used This keyword indicates that the type argument is a supertype of the bounding class So span class n List span span class o lt span span class w span span class kd super span span class w span span class n Number span span class o gt span could represent List lt Number gt or List lt Object gt Reading from a list defined as span class n List span span class o lt span span class w span span class kd super span span class w span span class n Number span span class o gt span returns elements of type Object Adding to such a list requires either elements of type Number any subtype of Number 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 12 Generics in throws clause editAlthough exceptions themselves cannot be generic generic parameters can appear in a throws clause public lt T extends Throwable gt void throwMeConditional boolean conditional T exception throws T if conditional throw exception Problems with type erasure editGenerics 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 List lt Integer gt will be converted to the non generic type List which ordinarily contains arbitrary objects The compile time check guarantees that the resulting code uses the correct type 7 Because of type erasure type parameters cannot be determined at run time 6 For example when an ArrayList is examined at runtime there is no general way to determine whether before type erasure it was an ArrayList lt Integer gt or an ArrayList lt Float gt Many people are dissatisfied with this restriction 14 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 final List lt Integer gt li new ArrayList lt gt final List lt Float gt lf new ArrayList lt gt 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 15 public class GenericException lt T gt extends Exception The reason why this is not supported is due to type erasure try throw new GenericException lt Integer gt catch GenericException lt Integer gt e System err println Integer catch GenericException lt String gt 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 lt T gt T instantiateElementType List lt T gt 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 Arrays editThere are several importance differences between arrays both primitive arrays and Object arrays and generics in Java Two of the major differences namely differences in terms of variance and reification Covariance contravariance and invariance edit Main article Covariance and contravariance computer science Generics 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 Object object and instantiates the object as a new Long object no compile time exception is thrown since arrays are covariant 6 This may give the false impression that the code is correctly written However if the developer attempts to add a String to this Long object the program will throw an ArrayStoreException 6 This run time exception can be completely avoided if the developer uses generics If the developer declares a Collection lt Object gt object an creates a new instance of this object with return type ArrayList lt Long gt the Java compiler will correctly throw a compile time exception to indicate the presence of incompatible types since generics are invariant 6 Hence this avoids potential run time exceptions This problem can be fixed by creating an instance of Collection lt Object gt using ArrayList lt Object gt object instead For code using Java SE7 or later versions the Collection lt Object gt can be instantiated with an ArrayList lt gt object using the diamond operator Reification edit 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 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 List lt T gt and List lt String gt where T is a generic formal parameter 6 Project on generics editProject 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 generic specialization e g List lt int gt reified generics making actual types available at runtime See also editGeneric programming Template metaprogramming Wildcard Java Comparison of C and Java Comparison of Java and C Citations edit Java Programming Language A ClassCastException can be thrown even in the absence of casts or nulls Java and Scala s Type Systems are Unsound PDF Bloch 2018 pp 123 125 Chapter 5 Item 27 Eliminate unchecked warnings GJ Generic Java Java Language Specification Third Edition by James Gosling Bill Joy Guy Steele Gilad Bracha Prentice Hall PTR 2005 a b c d e f g h i j k l m n o Bloch 2018 pp 126 129 Chapter 5 Item 28 Prefer lists to arrays a b c d e f g h Bloch 2018 pp 117 122 Chapter 5 Item 26 Don t use raw types Bloch 2018 pp 135 138 Chapter 5 Item 30 Favor generic methods Gilad Bracha July 5 2004 Generics in the Java Programming Language PDF www oracle com Type Inference for Generic Instance Creation Gilad Bracha July 5 2004 Generics in the Java Programming Language PDF www oracle com p 5 a b c d Bloch 2018 pp 139 145 Chapter 5 Item 31 Use bounded wildcards to increase API flexibility Bracha Gilad Wildcards gt Bonus gt Generics The Java Tutorials Oracle The sole exception is null which is a member of every type Gafter Neal 2006 11 05 Reified Generics for Java Retrieved 2010 04 20 Java Language Specification Section 8 1 2 Oracle Retrieved 24 October 2015 Goetz Brian Welcome to Valhalla OpenJDK mail archive OpenJDK Retrieved 12 August 2014 References editBloch Joshua 2018 Effective Java Programming Language Guide third ed Addison Wesley ISBN 978 0134685991 Retrieved from https en wikipedia org w index php title Generics in Java amp oldid 1192135436, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.