Contents - Cultural View

Contents - Cultural View Contents - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

Generics in Java 126 The use of wildcards above is necessary since objects of one type parameter cannot be converted to objects of another parameter. Neither List nor List is a subtype of the other, even though Integer is a subtype of Number. So, code that deals with List does not work with List. If it did, it would be possible to insert a Number that is not a Integer into it, which violates type safety. Here is a sample code that explains the contradiction it brings if List is a subtype of List: List ints = new ArrayList(); ints.add(2); List nums = ints; //valid if List is a subtype of List accordin 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. To specify the lower bounding class of a generic element, the super keyword is used. This keyword indicates that the aforementioned generic type is a super-type of said bounding class. So, List

Generics in Java 127 Pair grade440 = new Pair("mike", "A"); Pair marks440 = new Pair("mike", 100); System.out.println("grade: " + grade440); System.out.println("marks: " + marks440); Generic method definitions Here is an example of a generic method using the generic class above: public Pair twice(T value) { } return new Pair(value,value); In many cases the user of the method need not indicate the type parameters, as they can be inferred: Pair pair = twice("Hello"); The parameters can be explicitly added if needed: Pair pair = this.twice("Hello"); Note that you cannot use native types, ex: Pair pair; // this fails. You have to use Integer instead. Generics in throws clause Although exceptions themselves cannot be generic, generic parameters can appear in a throws clause: public void throwMeConditional { } (boolean conditional, T exception) throws T if(conditional) throw exception; Type erasure 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 will be converted to the non-generic type List, which can contain arbitrary objects. The compile-time check guarantees that the resulting code is type-correct. As a result of 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 or an ArrayList. 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 was presumably parameterized with Integer. Reflection can also determine the type parameter. However, no approach works in all cases. Demonstrating this point, the following code outputs "Equal": ArrayList li = new ArrayList(); ArrayList lf = new ArrayList(); if (li.getClass() == lf.getClass()) // evaluates to true

Generics in Java 127<br />

Pair grade440 = new Pair("mike", "A");<br />

Pair marks440 = new Pair("mike", 100);<br />

System.out.println("grade: " + grade440);<br />

System.out.println("marks: " + marks440);<br />

Generic method definitions<br />

Here is an example of a generic method using the generic class above:<br />

public Pair twice(T value)<br />

{<br />

}<br />

return new Pair(value,value);<br />

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

Pair pair = twice("Hello");<br />

The parameters can be explicitly added if needed:<br />

Pair pair = this.twice("Hello");<br />

Note that you cannot use native types, ex:<br />

Pair pair; // this fails. You have to use Integer instead.<br />

Generics in throws clause<br />

Although exceptions themselves cannot be generic, generic parameters can appear in a throws clause:<br />

public void throwMeConditional<br />

{<br />

}<br />

(boolean conditional, T exception) throws T<br />

if(conditional)<br />

throw exception;<br />

Type erasure<br />

Generics are checked at compile-time for type-correctness. The generic type information is then removed in a<br />

process called type erasure. For example, List will be converted to the non-generic type List, which can<br />

contain arbitrary objects. The compile-time check guarantees that the resulting code is type-correct.<br />

As a result of type erasure, type parameters cannot be determined at run-time. For example, when an ArrayList is<br />

examined at runtime, there is no general way to determine whether, before type erasure, it was an<br />

ArrayList or an ArrayList. Many people are dissatisfied with this restriction [5] . There are partial<br />

approaches. For example, individual elements may be examined to determine the type they belong to; for example, if<br />

an ArrayList contains an Integer, that ArrayList was presumably parameterized with Integer. Reflection can also<br />

determine the type parameter. However, no approach works in all cases.<br />

Demonstrating this point, the following code outputs "Equal":<br />

ArrayList li = new ArrayList();<br />

ArrayList lf = new ArrayList();<br />

if (li.getClass() == lf.getClass()) // evaluates to true

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!