14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Generics in Java 128<br />

System.out.println("Equal");<br />

Java generics differ from C++ templates. Java generics generate only one compiled version of a generic class or<br />

function regardless of the number of parameterizing types used. Furthermore, the Java run-time environment does<br />

not need to know which parameterized type is used because the type information is validated at compile-time and is<br />

not included in the compiled code. Consequently, instantiating a Java class of a parameterized type is impossible<br />

because instantiation requires a call to a constructor, which is unavailable if the type is unknown.<br />

For example, the following code will not compile:<br />

T instantiateElementType(List arg)<br />

{<br />

}<br />

return new T(); //causes a compile error<br />

Because there is only one copy per generic class at runtime, static variables are shared among all the instances of the<br />

class, regardless of their type parameter. As a result, the type parameter cannot be used in the declaration of static<br />

variables or in static methods. Static variables and static methods are "outside" of the scope of the class's<br />

parameterized types.<br />

See also<br />

• Generic programming<br />

• Java<br />

• Comparison of C# and Java<br />

• Comparison of Java and C++<br />

External links<br />

• Lesson: Generics [6] at The Java Tutorials<br />

• Generics [7] by Sun Microsystems<br />

• Wild FJ [8] by Mads Torgersen, Erik Ernst, and Christian Plesner Hansen<br />

• Java Generics FAQ [5] by Angelika Langer<br />

• JavaWorld Taming the Tiger [9] Excellent overview.<br />

• Java Generics Presentation [10] by DevelopIntelligence<br />

• Java Generics Tutorial [11] by Jakob Jenkov - jenkov.com<br />

References<br />

[1] Java Programming Language (http:/ / java. sun. com/ j2se/ 1. 5. 0/ docs/ guide/ language/ index. html)<br />

[2] Java Language Specification, Third Edition (http:/ / java. sun. com/ docs/ books/ jls/ third_edition/ html/ j3TOC. html) by James Gosling, Bill<br />

Joy, Guy Steele, Gilad Bracha - Prentice Hall PTR 2005<br />

[3] http:/ / java. sun. com/ j2se/ 1. 5/ pdf/ generics-tutorial. pdf - Generics in the Java Programming Language<br />

[4] http:/ / stackoverflow. com/ questions/ 156275/ what-is-the-equivalent-of-the-c-pairl-r-in-java<br />

[5] Gafter, Neal (2006-11-05). "Reified Generics for Java".<br />

[6] http:/ / java. sun. com/ docs/ books/ tutorial/ java/ generics/ index. html<br />

[7] http:/ / java. sun. com/ j2se/ 1. 5. 0/ docs/ guide/ language/ generics. html<br />

[8] http:/ / homepages. inf. ed. ac. uk/ wadler/ fool/ program/ 14. html<br />

[9] http:/ / www. javaworld. com/ javaworld/ jw-06-2004/ jw-0607-tiger2. html<br />

[10] http:/ / www. developintelligence. com/ learn/ tutorials. php<br />

[11] http:/ / tutorials. jenkov. com/ java-generics/ index. html

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

Saved successfully!

Ooh no, something went wrong!