java generics(under the hood of the compiler) by harmeet singh
DESCRIPTION
Generics are the powerful feature in Java programming language. In JDK 1.5 the Generics are introduce and it create new evolution in java. Generics are major topic and vast topic but in this we only care about some basics for generics.TRANSCRIPT
Java Generics(Under The Hood Of The Compiler)
By: Harmeet Singh (Taara)(Java EE Developer)
[email protected]://harmeetsingh13.blogspot.in
skype: harmeetsingh0013Contact: Via mail or Skype
Contents
1. Generic Type.2. WildCards in Generics.3. Unchecked Warnings.4. Heap Pollution.5. Type Eraser.6. Bridge method.7. Type Erasure of A Parameterized Type.8. Type Erasure of A Type Parameter.9. Type Erasure of A Generic Method.10.Reficiation.11. Reifilable Type.
Acknowledgement
● Special Thanks to “Angelika Langer” (Trainer & Author and Mentor). Everything about generics in this presentation is get from “Angelika Langer” Generics FAQ.
Generic Type● Type Parameter: Is a placeholder that will later be replace but a type
argument.● A parameterized or generic type is an instantiation of a generic type with
actual argument. ● Define Generics:
class Pair<S,T>{private S first;private T second;-----------------
}
Generic Type● Types that cannot have type parameter :
o Anonymous Inner Classeso Exception Typeso Enum Types.
● Generic Type Instantiated: o Pair<String, Long> pair = new Pair<String, Long>();
● Generics Declarations: o Collection<String> : Concrete Declaration.o Collection<?> : Wild Card Declaration.
WildCard in Generics● We are not create directly object whose types is wildcard parameterized
objectArrayList<?> list = new ArrayList<?>(); //error
● WildCard parameterized type is not declare in a supertypeclass WildCardParam<?>{ //error---------------------
}
WildCards In Generics● WildCard Types Family:
o “ ? “ : Unbounded WildCard o “ ? extends Type ” : WildCard With an upper boundo “ ? super Type “ : WildCard With an lower bound
WildCards in Generics● WildCards Bounds: A Reference type that is used to further describe the
family of types denoted by a wildcard. ● Difference between WildCard Bound and Type Parameter Bound:
o A WildCard can have only one bound, while a type parameter can have several bounds.
o A WildCard can have lower or an upper bound, while there is no such things as a lower bound for a types parameter.
WildCards In Generics● Does “extends” always means Inheritance ? : extends is an overloaded
keyword in java. it has different meanings, depending on the context it appears. The extends keyword appear in four different locations : o In the Definition of Class.o In the Definition of Interface.o In the Definition of Type Parameter bounds.o In the Definition of WildCards Bounds.
Unchecked Warnings● Compiler and the runtime system do not have enough type information to
perform all type checks that would be necessary to ensure type safety. example: TreeSet set = new TreeSet();
set.add(“abc”); //unchecked warnings. ● Xlint:unchecked enable “unchecked” warnings.● Xlint:-unchecked and @SuppressWarnings(“unchecked”) disable
“unchecked” warnings.
Heap Pollution● A Situation where a variable of parameterized types refer to an Object that
is not of that parameterized type. ● Reason of Heap Pollution Occur :-
1. Mixing Raw and Parameterized Type.2. Unwise Casting.3. Separate Compilation.
Type Erasure● How Compiler Translate Java Generics:
○ Compiler translate generic type or method(in any language, not just java) has in principle two choice. ■ Code Specialization : The compiler generates a new
representation of a generic type of method.■ Code Sharing : The compiler generates code for only one
representation of a generic type or method and maps all the instantiations of the generic type of method to the unique representation , perform type check and type conversion where needed.
Type Erasure● What is Type Erasure ?
o The compiler generates only one byte code representation of a generic type or method and maps all the instantiations of the generic type or method to the unique representation.
● The type Erasure process can be imagined as a translation from generic Java Source code back into regular Java Code.
● The Steps performed during type erasure : o Eliding Type Parameter : When the Compiler finds the definition of a
generic type or method, it removes all occurrences of the type parameters and replaces them by their leftmost, or type Object if no bounds specified. Ex: <T> var; into Object var;
Type Erasure ● The Steps performed during type erasure :
o Eliding Type argument: When the compiler finds a parameterized type, i.e an instantiation of a generic types, then it removes the type arguments. Like : List<String>, Set<Long> and Map<String, ?> are translated to List, Set and Map respectively.
Bridge Method● A Synthetic method that the compiler generates in the course of type
erasure. it is sometimes needed when a type extends or implements a parameterized class or interface.
Example Before type Erasure:
Bridge Method
Example After type Erasure:
● Side Effect of type erasure is that two methods have identical signature before type erasure and different signature after type erasure.
Bridge Method● Under which circumstances is a bridge method generated ?
o Bridge methods are necessary when a class implements a parameterized interface or extends a parametrized superclass and type erasure changes the arguments type of any of the inherited non-static method.
Bridge Method● The compiler must add the bridge methods even if the subclass does not
override the inherited method.
Type Erasure OF Parameterized Type
● The Erasure of a parameterized type is the type without any type argument (i.e the raw type). The definition extends to arrays and nested types.
Type Erasure Of Type Parameter● The type erasure of a type parameter is the erasure of its leftmost bound.
The type erasure of an unbounded type parameter is type Object. Examples :
Type Erasure Of Generic Type● The Erasure of a method signature consisting of the same name and the
erasures of all the formal method parameters types.Examples :
Reficiation● Representing type parameters and arguments of generic types and
method at runtime. Reficiation is the opposite of type Erasure.
Reifiable● A type whose type information is fully available at run time, that is, a type that does not lose
information in the course of type erasure. ● The following types are reifiable:
o primitive typeso non-generic (or non-parameterized) reference typeso unbounded wildcard instantiationso raw typeso arrays of any of the above
● The non-reifiable types, which lose type information as a side effect of type erasure, are:o instantiations of a generic type with at least one concrete type argumento instantiations of a generic type with at least one bounded wildcard as type argument
● Reifiable types are permitted in some places where non-reifiable types are disallowed.o as type in an instanceof expressiono as component type of an array
References
● http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html