GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Generic Types
  • Generic Methods
  • Type Parameters
  • Type Arguments

This view

  • Original page
  • New address

GeezerVison is showing angelikalanger.com — AngelikaLanger.com - Java Generics FAQs - Frequently Asked Questions - Angelika Langer Training/Consulting

 
                       Angelika Langer - Training & Consulting
HOME | COURSES | TALKS | ARTICLES | GENERICS | LAMBDAS | IOSTREAMS | ABOUT | CONTACT | Twitter | Lanyrd | Linkedin
Books  
HOME 

GENERICS
  FAQ    
    CONTENT
    FUNDAMENTALS
    FEATURES
    PRACTICAL
    TECHNICAL
    INFORMATION
    GLOSSARY
    INDEX
    PDF VERSION
 
LAMBDAS 
IOSTREAMS 
ABOUT 
CONTACT 
Java Generics FAQs - Frequently Asked Questions

This is a collection of answers to frequently asked questions (FAQs) about Java Generics, a new language feature added to the Java programming language in version 5.0 of the Java Standard Edition (J2SE 5.0).

If you want to provide feedback or have any 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.
A printable version of the FAQ documents is available in PDF format (4.5MB).

Java Generics FAQs - Frequently Asked Questions

About This FAQ

© Copyright  2004-2022 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 is. 

"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 this 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, which will become the more interesting the more you learn 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 works in presence of generic methods. 

In addition to these four 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 Preface.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 Preface.FAQ002
REFERENCES

Whom is the FAQ for?

For practicing Java programmers who have to do with Java Generics.
First a disclaimer: the FAQ does not aim to be mainly a tutorial.  Tutorials can be found elsewhere.  It has tutorial style answers, but it goes beyond what a casual or first-time generics user might want to know about Java Generics.  On the other hand, it is still written for humans, as opposed to compiler builders or computer science theorists whose pet issue is type theory.  While type theory is an interesting topic, the main target audience is programmers who use or want to use generics in practice. 

The FAQ is supposed to grow and mature over time.  In particular the section on programming techniques using generics will hopefully gain weight as the body of experience with Java generics grows.  If you have a technique or observation made in practice that you want to share with other programmers, feel free to send email and suggest it for inclusion in the FAQ. 

LINK TO THIS Preface.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 Preface.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 Preface.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


Practicalities - Programming With Java Generics

Using Generic Types
Using Generic Methods
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
    Wildcard Capture
    Wildcard Instantiations
    Cast and instanceof


More Information on Java Generics

Glossary

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Index
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

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?
Language Features of Java Generics
    Which language features are related to Java generics?

    Generic Types

    Fundamentals
    • 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
    • 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
    • What is the raw type?
    • Can I use a raw type like any other type?
    Wildcard Instantiations
    • 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 I 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>> "?
      • Why is there no lower bound for type parameters?
      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
    • 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 collections?
    • What is a checked collection?
    • What is the difference between a Collection<?> and a Collection<Object>?
    • How do I express that a collection is a mix of objects of different types?
    • What is the difference between a Collection<Pair<String,Object>>, a Collection<Pair<String,?>> and a Collection<? extends Pair<String,?>>?
    • How can I make sure that the same wildcard stand for the same type?
    Using Generic Methods
    • Why doesn't method overloading work as I expect it?
    • Why doesn't method overriding work as I expect it?
    Coping With Legacy
    • What happens when I mix generic and non-generic code?
    • Should I re-engineer all my existing classes and generify them?
    • How do I generify an existing non-generic type or method?
    • Can I safely generify a supertype, or does it affect all subtypes?
    • How do I avoid breaking binary compatibility when I generify an existing type or method?
    Defining Generic Types and Methods
    • Which types should I design as generic types instead of defining them as regular non-generic types?
    • Do generics help designing parallel class hierarchies?
    • When would I use an unbounded wildcard instantiation instead of a bounded or concrete instantiation?
    • When would I use a wildcard instantiation instead of a concrete instantiation?
    • When would I use a wildcard instantiation with an lower bound?
    • How do I recover the actual type of the this object in a class hierarchy?
    • What is the "getThis" trick?
    • How do I recover the element type of a container?
    • What is the "getTypeArgument" trick?
    Designing Generic Methods
    • Why does the compiler sometimes issue an unchecked warning when I invoke a "varargs" method?
    • What is a "varargs" warning?
    • How can I suppress a "varargs" warning?
    • When should I refrain from suppressing a "varargs" warning?
    • Which role do wildcards play in method signatures?
    • Which one is better: a generic method with type parameters or a non-generic method with wildcards?
    • Under which circumstances are the generic version and the wildcard version of a method equivalent?
    • Under which circumstances do the generic version and the wildcard version of a method mean different things?
    • Under which circumstances is there no transformation to the wildcard version of a method possible?
    • Should I use wildcards in the return type of a method?
    • How do I implement a method that takes a wildcard argument?
    • How do I implement a method that takes a multi-level wildcard argument?
    • I want to pass a U and a X<U> to a method.  How do I correctly declare that method?
    Working With Generic Interfaces
    • Can a class implement different instantiations of the same generic interface?
    • Can a subclass implement another parameterized interface than any of its superclasses does?
    • What happens if a class implements two generic interfaces that define the same method?
    • Can an interface type nested into a generic type use the enclosing type's type parameters?
    Implementing Infrastructure Methods
    • How do I best implement the equals method of a generic type?
    • How do I best implement the clone method of a generic type?
    Using Runtime Type Information
    • What does the type parameter of class java.lang.Class mean?
    • How do I pass type information to a method so that it can be used at runtime?
    • How do I generically create objects and arrays?
    • How do I perform a runtime type check whose target type is a type parameter?
    Reflection
    • Which information related to generics can I access reflectively?
    • How do I retrieve an object's actual (dynamic) type?
    • How do I retrieve an object's declared (static) type?
    • What is the difference between a generic type and a parameterized type in reflection?
    • How do I figure out whether a type is a generic type?
    • Which information is available about a generic type?
    • How do I figure out whether a type is a parameterized type?
    • Which information is available about a parameterized type?
    • How do I retrieve the representation of a generic method?
    • How do I figure out whether a method is a generic method?
    • Which information is available about a generic method?
    • Which information is available about a type parameter?
    • What is a generic declaration?
    • What is a wildcard type?
    • Which information is available about a wildcard?

Technicalities - Under the Hood of the Compiler
    Compiler Messages
    • What is an "unchecked" warning?
    • How can I disable or enable unchecked warnings?
    • What is the -Xlint:unchecked compiler option?
    • What is the SuppressWarnings annotation?
    • How can I avoid "unchecked cast" warnings?
    • Is it possible to eliminate all "unchecked" warnings?
    • Why do I get an "unchecked" warning although there is no type information missing?
    Heap Pollution
    • What is heap pollution?
    • When does heap pollution occur?
    Type Erasure
    • How does the compiler translate Java generics?
    • What is type erasure?
    • What is reification?
    • What is a bridge method?
    • Under which circumstances is a bridge method generated?
    • Why does the compiler add casts when it translates generics?
    • How does type erasure work when a type parameter has several bounds?
    • What is a reifiable type?
    • What is the type erasure of a parameterized type?
    • What is the type erasure of a type parameter?
    • What is the type erasure of a generic method?
    • Is generic code faster or slower than non-generic code?
    • How do I compile generics for use with JDK <= 1.4?
    Type System
    • How do parameterized types fit into the Java type system?
    • How does the raw type relate to instantiations of the corresponding generic type?
    • How do instantiations of a generic type relate to instantiations of other generic types that have the same type argument?
    • How do unbounded wildcard instantiations of a generic type relate to other instantiations of the same type?
    • How do wildcard instantiations with an upper bound relate to other instantiations of the same type?
    • How do wildcard instantiations with a lower bound relate to other instantiations of the same type?
    • Which super-subtype relationships exist among instantiations of generic types?
    • Which super-subset relationships exist among wildcards?
    • Does "extends" always mean "inheritance"?
    Exception Handling
    • Can I use parameterized types in exception handling?
    • Why are generic exception and error types illegal?
    • Can I use a type parameter in exception handling?
    • Can I use a type parameter in a catch clause?
    • Can I use a type parameter in in a throws clause?
    • Can I throw an object whose type is a type parameter?
    Static Context
    • How do I refer to static members of a parameterized type?
    • How do I refer to a (non-static) inner class of a parameterized type?
    • How do I refer to an interface type nested into a parameterized type?
    • How do I refer to an enum type nested into a parameterized type?
    • Can I import a particular instantiation of generic type?
    • Why are generic enum types illegal?
    Type Argument Inference
    • What is type argument inference?
    • Is there a correspondence between type inference for method invocation and type inference for instance creation?
    • What is the "diamond" operator?
    • What is type argument inference for instance creation expressions?
    • Why does the type inference for an instance creation expression fail?
    • What is type argument inference for generic methods?
    • What explicit type argument specification?
    • Can I use a wildcard as the explicit type argument of a generic method?
    • What happens if a type parameter does not appear in the method parameter list?
    • Why doesn't type argument inference fail when I provide inconsistent method arguments?
    • Why do temporary variables matter in case of invocation of generic methods?
    Wildcard Capture
    • What is the capture of a wildcard?
    • What is the capture of an unbounded wildcard compatible to?
    • Is the capture of a bounded wildcard compatible to the bound?
    Wildcard Instantiations
    • Which methods and fields are accessible/inaccessible through a reference variable of a wildcard type?
    • Which methods that use the type parameter in the argument or return type are accessible in an unbounded wildcard instantiation?
    • Which methods that use the type parameter in the argument or return type are accessible in an upper bound wildcard instantiation?
    • Which methods that use the type parameter in the argument or return type are accessible in a lower bound wildcard instantiation?
    • Which methods that use the type parameter as type argument of a parameterized argument or return type are accessible in a wildcard instantiation?
    • Which methods that use the type parameter as upper wildcard bound in a parameterized argument or return type are accessible in a wildcard instantiation?
    • Which methods that use the type parameter as lower wildcard bound in a parameterized argument or return type are accessible in a wildcard instantiation?
    • In a wildcard instantiation, can I read and write fields whose type is the type parameter?
    • Is it really impossible to create an object whose type is a wildcard parameterized type?
    Cast and instanceof
    • Which types can or must not appear as target type in an instanceof expression?
    Overloading and Overriding
    • What is method overriding?
    • What is method overloading?
    • What is the  @Override annotation?
    • What is a method signature?
    • What is a subsignature?
    • What are override-equivalent signatures?
    • When does a method override its supertype's method?
    • What are covariant-return types?
    • What are substitutable return types?
    • Can a method of a non-generic subtype override a method of a generic supertype?
    • Can a method of a generic subtype override a method of a generic supertype?
    • Can a generic method override a generic one?
    • Can a non-generic method override a generic one?
    • Can a generic method override a non-generic one?
    • What is overload resolution?
More Information on Java Generics
Where can I find a generics tutorial?
Where can I find a specification of the Java generics language features?
Which books cover Java generics?
What webpages are devoted to Java generics?
Glossary 
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Index
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Acknowledgements

The FAQ is written and maintained by Angelika Langer . 

Klaus Kreft, Bruce Eckel and Cay Horstmann reviewed this FAQ and provided invaluable feedback. 

A number of individuals at (or formerly at) Sun Microsystems (now Oracle) patiently answered countless questions I posed regarding Java generics.  My thanks to Gilad Bracha, Neal Gafter, and Peter von der Ahé. 

Suggestions and corrections, that inspired changes in this FAQ, have been provided by the following people (in alphabetic order): Eric Armstrong, Richard Grin, Markus Keller, Richard Kennard, Sascha Kratky, Keith Lea, Mike Lehmann, Maurice Naftalin, Joe Soroka, Frank Tip, Phil Wadler.


 

Change Log

Below is a list of minor and major modifications of the FAQ. 
 
October 2004
First release.
January 2005
Terminology change. 

A reader pointed out that the draft of JLS 3 uses terms different from what I use in this FAQ document.  This is because I started writing the FAQ before even a draft of JLS 3 was available. There still is no final specification of the language features of Java Generics.  Nonetheless I decided to reduce the mismatch somewhat by changing some of the terms used throughout this document. 

The most significant deviation from the JLS 3 terminology is the use of the term "parameterized type".  The JLS uses the term "parameterized type" to refer to types with a type argument list, such as List<String> or List<?> . I used the term "parameterized type" to distinguish between types with type parameters, such as List , which is a type that declares a type parameter E that stands for the type of the elements in the list, and plain old types without type parameters, such as String , Date , Point , etc.  Hence I made the following change: 
 

 
formerly used in this FAQ => now used in this FAQ (and in JLS 3)
parameterized type/method => generic type/method
instantiation of a parameterized type  => parameterized type

There remains an issue regarding use of the term "instantiation".  The term "instantiation" is used in the JLS in conjunction with object creation:  a type is instantiated to form an instance of the type, that is, an object of that type.  I reuse the term "instantiation" to refer to the "creation" of a parameterized type from a generic type, that is, for the manifestation of a generic type such as List with its formal type parameter E as a parameterized type with an actual type argument as in List<String> or List<?> .  This can be regarded as misleading, because nothing is created, not even a new type, different from what happens in C++ for instance when a template is instantiated. Yet I could not think of a more precise or adequate term that would denote the fact that formal type parameters are replaced by actual type arguments in a generic type or method.  In other words, I "instantiate" generic types to form parameterized types, and I talk of "instantiations" of a generic type, which are the parameterized type, as opposed to "instances" of a type, which would be objects, like in the JLS.

May 2005 
Updated the reference section; since April 19, 2005 the Java Language Specification, 3rd edition is available on the web at: http://java.sun.com/docs/books/jls/
August 2005 
Corrected a couple of bugs. Affected are TypeArguments.FAQ103 , TypeArguments.FAQ104 , TechnicalDetails.FAQ501 , TechnicalDetails.FAQ502 , ProgrammingIdioms.FAQ201 , ProgrammingIdioms.FAQ303 . 
Added some new items: ProgrammingIdioms.FAQ006A , ProgrammingIdioms.FAQ050 , ProgrammingIdioms.FAQ302A thru ProgrammingIdioms.FAQ302C , TechnicalDetails.FAQ050 , TechnicalDetails.FAQ051 . 

Added a glossary in order to get the terminology straight.  I noticed that documents originating from Sun Microsystems, such as the language specification ( JLS 3 ) and the tutorial , use slightly different terms than you will find in books and articles from other sources. The glossary aims to list commonly used terms related to Java Generics and to explain what they denote and which ones are synonyms.  The controversial terms are invocation/instantiation of generic/parameterized types/methods.  The JLS talks of an invocation of a generic type or method, where most other authors talk of an instantiation of a generic type or method.  Some authors equate generic type and parameterized type, while the JLS equates invocation and parameterized type. 

October 2005 
Added some stuff on overloading and overriding in the context of Java generics. The new items are: ProgrammingIdioms.FAQ051 and TechnicalDetails.FAQ801 thru TechnicalDetails.FAQ830 . 
Further additions: ProgrammingIdioms.FAQ103A , ProgrammingIdioms.FAQ201A , ProgrammingIdioms.FAQ300 , TechnicalDetails.FAQ007 , TechnicalDetails.FAQ402A , TechnicalDetails.FAQ609 . 
December 2005 
Added new item: TypeParameters.FAQ107 . 
Made available a PDF version of this FAQ document.
May 2006
Added new item: ProgrammingIdioms.FAQ205 . 
November 2006 
Added an additional example to item ProgrammingIdioms.FAQ300 .  My thanks to Peter von der Ahé and Bob Lee for supplying the example.
December 2006
Added whole new section on Java Generics and Reflection: ProgrammingIdioms.Reflection . 
Corrected a mistake in entry TechnicalDetails.FAQ821 . My thanks to Jingyi Wang for spotting the mistake.
May 2007
Corrected some typos.  My thanks to Oleksii Shurubura and Zhang Xu for spotting the bugs.
Added new item describing the "getThis" trick: ProgrammingIdioms.FAQ206 . 
October 2007
Corrected some typos.  My thanks to Steven Adams and Seb Rose for spotting the bugs.
February 2008
Corrected some typos.  My thanks to Liu Yuqing for spotting the bugs.
German speaking readers might be interested in a series of articles that I made available at EffectiveJava .The articles are drafts of contributions to our "Effective Java" column published in a German print magazine in 2007.
March 2008
Corrected some typos.  My thanks to Marcel Rüedi for spotting the bugs.
April 2008
Replaced an incorrect example in TypeParameters.FAQ402 by a more useful one and corrected a bug in TypeParameters.FAQ105 . My thanks to Jürgen Jäschke and Martin Goerg for spotting the bugs.
July 2008
Corrected some typos.  My thanks to Rob Ross for spotting the bugs.
Added some links and bookmarks to the PDF version of this FAQ document for better navigation. It is not perfect, but I hope it helps.
August 2008
Added a link and an additional example to Practicalities.FAQ603 . My thanks to Dan Frankowski for suggesting the addition.
Rewrote Technicalities.FAQ402A because the compilers nowadays apply smarter type inference strategies than they did when the entry was written; as a result some of the source code examples were no longer meaningful. My thanks to Steven Busack for bringing the changes to my attention. 
Added two new entries Practicalities.FAQ207 and Practicalities.FAQ208 that describe a programming technique that Jesse Glick brought to my attention.  Kudos to Jesse.
October 2008
Corrected some typos.  My thanks to Gary Gregory for spotting the bugs.
August 2009
Corrected some typos.  My thanks to Ganesh Hegde and Barry Soroka for spotting the bugs.
June 2010
Corrected bugs in Technicalities.FAQ103 and Technicalities.FAQ608 .  My thanks to Clive Scott for spotting the bugs.
August 2010
Made an update to acknowledge additions and corrections intended for release with Java 7. Specifically, I added a couple of entries regarding the problem with varargs and generics, an explanation of the new varargs warning, and whether or not one can safely suppress the varargs warnings (see Practicalities.FAQ300A ). In addition, I added a section on the diamond operator, the improved type inference for instance creation expressions and its limitations (see Technicalities.FAQ400 ).  My thanks to Maurizio Cimadamore of Oracle for a couple of clarifications. 
Corrected a typo.  My thanks to Prasanth Jalasutram for spotting the typo.
May 2011
Corrected minor omission in Technicalities.FAQ824 .  My thanks to Gene Carpenter for spotting the omission.
August 2011
Made a couple of minor adjustments in Practicalities.FAQ300A , Practicalities.FAQ300B , and Practicalities.FAQ300C due to differences between the prototype implementation and the final release of Java 7.  Specifically, the annotation @SuppressWarnings("varargs") has been replaced by the @SafeVarargs annotation and the "varargs" warnings became plain "unchecked" warnings.
February 2012
Corrected some formatting and grammar issues.  My thanks to Chris Dailey and Alan Frankel for spotting the deficiencies.
November 2012
Updated the section on related information such as books and websites (see Information.FAQ002 and InformationFAQ005 ).
February 2013 
Corrected an error in Technicalities.FAQ400D regarding use of the diamond operator on anonymous inner classes. My thanks to Rabea Gransberger for spotting the bug.
April 2013 
Updated the sections related to type parameter inference for invocation of generic methods and the diamond operator.  Type inference has been changed and improved substantially for Java 8.  Numerous situations that resulted in error messages in Java  5, 6, and 7 compile just fine in Java 8.  The changes concern Technicalities.FAQ400C , Technicalities.FAQ400D , Technicalities.FAQ403 , Technicalities.FAQ405 .
Updated entry TypeParameters.FAQ107 for clarification.  The entry discusses the lack of lower bounds on type parameters, i.e., a feature that does not (yet) exist, but is occasionally considered useful for certain situations. I added examples in order to illustrate when the feature would be harmful or useful.
February 2014
Fixed an incorrect example in TypeParameters.FAQ102 . My thanks to Dheeru Mundluru for spotting the bug.
August 2014 
A couple of corrections suggested by Moez AbdelGawad.
January 2015
Type inference was slightly modified in Java 8 which affected the example in Technicalities.FAQ404 .  My thanks to Chad Berchek for bringing it to my attention and to Dan Smith of Oracle for explaining the type inference modification to me.
March 2015
Fixed a couple of typos in Technicalities.FAQ823 . My thanks to José María García Varona for spotting the glitches.
February 2016
Updated some sections to reflects language changes in Java 9: 
  • The diamond operator is permitted in anonymous class definitions ( Technicalities.FAQ400B ).
  • The @SafeVarags annotation is permitted on private instance methods ( Practicalities.FAQ300B ).
  • August 2016
    Uploaded a bunch of missing diagrams.  My thanks to Xavier Tarrago for spotting the omission on my website.
    April 2017
    Added a missing topic in the table of contents.  My thanks to Dan Fabulich for spotting the omission.
    August 2017
    Fixed a typo ( Practicalities.FAQ501 ). My thanks to Satrajit Acharyya for spotting it.
    October 2017
    Fixed a typo ( Technicalities.FAQ604 ). My thanks to Alessio Gaspar for spotting it.
    August 2018
    Fixed a misleading example ( TypeArguments.FAQ203 ). My thanks to Rishabh Gupta for bringing it to my attention.
    Oktober 2018
    Fixed a typo ( GenericTypes.FAQ104A ). My thanks to Rinat Akhmetov for spotting it.
    May 2021
    Fixed a typo ( TypeArguments.FAQ201 ). My thanks to Kent Hervey for spotting it.

     

    Copyright Notice

    All text and content found at URLs starting with http://www.AngelikaLanger.com/GenericsFAQ/ (collectively, "the Java Generics FAQ") are the sole property of  Angelika Langer. Copyright @ 2004-2021 by Angelika Langer .  All rights reserved.

    Except as specifically granted below, you may not modify, copy, publish, sell, display, transmit (in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise), adapt, distribute, store in a retrieval system, create derivative works, or in any other way use or exploit the contents of the Java Generics FAQ, without the prior consent of the author. 

    All rights, titles and interest, including copyrights and other applicable intellectual property rights, in any of the material belongs to the provider of the material. You do not acquire proprietary interest in such materials by accessing them on my web site. You must abide by all copyright notices and restrictions on use of the material accessed. 

    In particular, I do NOT grant permission to copy the Java Generics FAQ or any part of it to a public Web server. Link to the original pages instead. (The problem with you putting a page on your server is that the search engines will find it and send my readers to your server instead. Thus they will be deprived of the most up-to-date version of the document.) 

    Non-commercial Use
    As a reader of the Java Generics FAQ, you are granted a nonexclusive, nontransferable, limited license to access and use for non-commercial (research and information) purposes the materials that are made available to you as the Java Generics FAQ. This license includes the right to download and print out one copy of the Java Generics FAQ so long as you neither change nor delete any author attribution, legend or copyright notice.  I request that you do not break up the document and attribute the source in such a way that someone can find the most up-to-date version on the Web, e.g. by including a link to http://www.AngelikaLanger.com/GenericsFAQ/JavaGenericsFAQ.html. If you redistribute the hardcopy of the Java Generics FAQ, please send me email informing me of the usage. 

    Other forms of non-commercial use may be allowed with prior written permission.  If permission is granted for non-commercial use, credit to the copyright owner must be displayed as prescribed by copyright owner in every exposure of text. To request non-commercial use of copyrighted materials, please email your request and provide the following information:  your name, organization, and title, your request (the text or texts you wish to quote) and the non-commercial purpose of the use. 

    Commercial Use

    Commercial use on paper, electronic storage devices of all types, online via the Internet or any part of the World Wide Web or via any other electronic distribution is strictly prohibited without prior payment to the copyright owner, as well as attribution of ownership of copyright and URL in every exposure of text. To negotiate legal use of text owned and copyrighted by Angelika Langer, please send me email . 

    Disclaimers

    The author has taken care in the preparation of this material, but makes no expressed or implied warranty of any kind and assumes no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the information or programs contained herein. 
     




    CONTENT NEXT INDEX
     
      © Copyright 1995-2022 by Angelika Langer.  All Rights Reserved.    URL: < http://www.AngelikaLanger.com/GenericsFAQ/JavaGenericsFAQ.html>    last update: 13 May 2021
    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.