This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Java Generics Faq as PDF for free.
This is a snapshot of the online Java Generics FAQ in printable form. The most up-to-date version of the oringal document can be found at www.AngelikaLanger.com/GenericsFAQ/JavaGenericsFAQ.html.
All text and content found in this document and online at URLs starting with http://www.AngelikaLanger.com/GenericsFAQ/ (collectively, "the Java Generics FAQ") are the sole property of Angelika Langer. Copyright @ 2004-2008 by Angelika Langer. All rights reserved.
How is this FAQ organized at large? How are the individual answered questions organized? Whom is the FAQ for? How do I best read the FAQ - top-to-bottom or zig-zag? Whom do I contact when I have questions, comments or suggestions related to this FAQ? Table Of Contents Acknowledgements Change Log Copyright Notice
How is this FAQ organized at large? The FAQ has 4 major sections: • • • •
Fundamentals of Java Generics Language Features of Java Generics Practicalities - Programming With Java Generics Technicalities - Under the Hood of the Compiler
"Fundamentals of Java Generics" explains the very basics of Java generics, what generics are and what their purpose and benefit it. "Language Features of Java Generics" is a huge section that explains all language features of Java Generics: • • • •
Generic Types Generic Methods Type Parameters Type Arguments
This section can be seen as the theoretical foundation and lays the ground for understanding more practical and more advanced issues. "Practicalities - Programming With Java Generics" aims to address issues that arise when you use Java Generics in practice. It discusses topics such as "How do I design a generics API?" , "Should my class be generic or not?", "What happens when I mix generic and non-generic Java?", "Why is class Class generic and what is good for?". The focus is on programming techniques on the one hand and pitfalls and surprises on the other hand. It is likely that this section will grow over time as programming idioms and techniques using Java Generics will be discovered.
"Technicalities - Under the Hood of the Compiler" addresses more advanced and more esoteric aspects of Java Generics, that will become the more interesting the more you learn about about Java Generics. Some of these topics are typically of little interest when you begin using generics. For instance, the section explains what the notorious "unchecked" warnings are, what translation by type erasure is, what wildcard capture and type inference are, what the type system looks like now that it includes generic types, and how overload resolution work in presence of generic methods. In addition to these 4 main sections the FAQ has a Table Of Contents, a reference to More Information on Java Generics such as tutorials, books, articles, and websites, a Glossary, and an alphabetic Index. LINK TO THIS
#FAQ001
REFERENCES
How are the individual answered questions organized? There is a question, a short answer and a sometimes fairly long answer, plus a cross reference section. I've found that many readers underestimate the reference section - to their own detriment. The cross references refer to related items and point in particular to entries that explain terms or facts mentioned in the text, but not explained there. In other words, if there is something in the explanation that has not been explained yet, go to the reference section and see whether you can find a reference to a corresponding entry that explains it. If not, let me know. LINK TO THIS
want to share with other programmers, feel free to send email and suggest it for inclusion to the FAQ. LINK TO THIS
#FAQ003
REFERENCES
Where can I find a generics tutorial? Whom do I contact when I have questions, comments or suggestions related to this FAQ?
How do I best read the FAQ - top-to-bottom or zig-zag? Both should be possible. The FAQ is kind of organized in a way that you can read it sequentially, starting with the first question until the bitter end. However, you will find that occasional jumps back and forth will be inevitable. For instance, when the language features are explained the term type erasure will be mentioned. Type erasure is part of what the compiler does during compilation and it is discussed in detail in the Technicalities section. So you will probably jump forward to the explanation of type erasure to see what it is in principle, spare yourself the details, and return to the language feature from which you started your excursion to the Technicalities section. Beyond that, the FAQ is mainly intended as a reference and can be consulted any order you feel like. Related topics are grouped together so that it might be convenient to read certain sections that are of immediate interest and ignore the rest for the time being. You might find that the same fact is explained repeatedly in different entries. This is intended. A certain amount of redundency is inevitable in order to make it possible to use the FAQ as a reference. If you feel you already know this, skip it and move on. Also, if you use the FAQ as a reference, the table of contents and the index should help finding what you are looking for. If not. let me know. LINK TO THIS
#FAQ004
REFERENCES
Table Of Contents Index Whom do I contact when I have questions, comments or suggestions related to this FAQ?
Whom do I contact when I have questions, comments or suggestions related to this FAQ? If you want to provide feedback or have questions regarding Java generics, to which you cannot find an answer in this document, feel free to send me EMAIL or use the GENERICS FAQ form. I might not be capable of answering in a timely manner or perhaps not at all, but I will try. LINK TO THIS
#FAQ005
REFERENCES
Table Of Contents About This FAQ Fundamentals of Java Generics Language Features of Java Generics Generic Types Fundamentals Concrete Instantiations Raw Types Wildcard Instantiations Generic Methods Fundamentals Type Parameters Fundamentals Type Parameter Bounds Usage Scope Static Context Type Arguments Fundamentals Wildcards Wildcard Bounds
Coping With Legacy Defining Generic Types and Methods Designing Generic Methods Working With Generic Interfaces Implementing Infrastructure Methods Using Runtime Type Information Reflection
Technicalities - Under the Hood of the Compiler Compiler Messages Heap Pollution Type Erasure Type System Exception Handling Static Context Type Argument Inference Wilcard Capture Wildcard Instantiations Cast and instanceof Overload Resolution
More Information on Java Generics Glossary Index
About This FAQ How is this FAQ organized at large? How are the individual answered questions organized? Whom is the FAQ for? How do I best read the FAQ - top-to-bottom or zig-zag? Whom do I contact when I have questions, comments or suggestions related to this FAQ?
Fundamentals of Java Generics What are Java generics? What is the primary purpose of Java generics? What is the benefit of using Java generics? What does type-safety mean?
What is a generic or parameterized type? How do I define a generic type? Are there any types that cannot have type parameters? How is a generic type instantiated (to form a parameterized type)? Why do instantiations of a generic type share the same runtime type? Can I cast to a parameterized type? Can I use parameterized types in exception handling? Can generic types have static members?
Concrete Instantiations o o o o o o o
What is a concrete instantiation of a generic type? Are different concrete instantiations of the same generic type compatible? Can I use a concrete parmeterized type like any other type? Can I create an array whose component type is a concrete parameterized type? Can I declare a reference variable of an array type whose component type is a concrete parameterized type? How can I work around the restriction that there are no arrays whose component type is a concrete parameterized type? Why is there no class literal for the concrete parameterized type?
Raw Types o o
What is the raw type? Can I use a raw type like any other type?
Wildcard Instantiations o o o o o o o o o o
What is a wildcard instantiation of a generic type? What is an unbounded wildcard instantiation of a generic type? Which methods and fields are accessible/inaccessible through a reference variable of a wildcard parameterized type? What is the difference between the unbounded wildcard parameterized type and the raw type? Can I use a wildcard parameterized type like any other type? Can I create an object whose type is a wildcard parameterized type? Can I create an array whose component type is a wildcard parameterized type? Can I declare a reference variable of an array type whose component type is a bounded wildcard parameterized type? Why is it allowed to create an array whose component type is an unbounded wildcard parameterized type? Can I declare a reference variable of an array type whose component type is an
unbounded wildcard parameterized type? Can I derive from a wildcard instantiation of a parameterized type? Why is there no class literal for wildcard parameterized types?
Generic Methods Fundamentals
What is a generic method? How do I invoke a generic method?
Type Parameters Fundamentals
What is a type parameter? Where is a type parameter visible (or invisible)?
Type Parameter Bounds
What is a bounded type parameter? Which types are permitted as type parameter bounds? Can I use a type parameter as a type parameter bound? Can I use different instantiations of a same generic type as bounds of a type parameter? How can work around the restriction that a type parameter cannot have different instantiations of a same generic type as its bounds? Does a bound that is a class type give access to all its public members? How do I decrypt "Enum<E extends Enum<E>>"?
Usage
Can I use a type parameter like a type? Can I create an object whose type is a type parameter? Can I create an array whose component type is a type parameter? Can I cast to the type that the type parameter stands for? Can I use a type parameter in exception handling? Can I derive from a type parameter? Why is there no class literal for a type parameter?
Scope
Where is a type parameter visible (or invisible)? Can I use a type parameter as part of its own bounds or in the
declaration of other type parameters? Static Context
Is there one instances of a static field per instantiation of a generic type? Why can't I use a type parameter in any static context of the generic class?
Type Arguments Fundamentals
What is a type argument? Which types are permitted as type arguments? Are primitive types permitted as type arguments? Are wildcards permitted as type arguments? Are type parameters permitted as type arguments? Do I have to specify a type argument when I want to use a generic type? Do I have to specify a type argument when I want to invoke a generic method?
Wildcards
What is a wildcard? What is an unbounded wildcard? What is a bounded wildcard? What do multi-level wildcards mean? If a wildcard appears repeatedly in a type argument section, does it stand for the same type?
Wildcard Bounds
What is a wildcard bound? Which types are permitted as wildcard bounds? What is the difference between a wildcard bound and a type parameter bound?
Practicalities - Programming With Java Generics Using Generic Types o o o
Should I prefer parameterized types over raw types? Why shouldn't I mix parameterized and raw types, if I feel like it? Should I use the generic collections or better stick to the old non-generic