Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java generics(Under The Hood Of The Compiler) by Harmeet singh


Published on

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.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Java generics(Under The Hood Of The Compiler) by Harmeet singh

  1. 1. Java Generics (Under The Hood Of The Compiler) By: Harmeet Singh (Taara) (Java EE Developer) skype: harmeetsingh0013 Contact: Via mail or Skype
  2. 2. 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.
  3. 3. Acknowledgement ● Special Thanks to “Angelika Langer” (Trainer & Author and Mentor). Everything about generics in this presentation is get from “Angelika Langer” Generics FAQ.
  4. 4. 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; ----------------- }
  5. 5. Generic Type ● Types that cannot have type parameter : o Anonymous Inner Classes o Exception Types o 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.
  6. 6. WildCard in Generics ● We are not create directly object whose types is wildcard parameterized object ArrayList<?> list = new ArrayList<?>(); //error ● WildCard parameterized type is not declare in a supertype class WildCardParam<?>{ //error --------------------- }
  7. 7. WildCards In Generics ● WildCard Types Family: o “ ? “ : Unbounded WildCard o “ ? extends Type ” : WildCard With an upper bound o “ ? super Type “ : WildCard With an lower bound
  8. 8. 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.
  9. 9. 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.
  10. 10. 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.
  11. 11. 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.
  12. 12. 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.
  13. 13. 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;
  14. 14. 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.
  15. 15. 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:
  16. 16. 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.
  17. 17. 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.
  18. 18. Bridge Method ● The compiler must add the bridge methods even if the subclass does not override the inherited method.
  19. 19. 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.
  20. 20. 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 :
  21. 21. 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 :
  22. 22. Reficiation ● Representing type parameters and arguments of generic types and method at runtime. Reficiation is the opposite of type Erasure.
  23. 23. 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 types o non-generic (or non-parameterized) reference types o unbounded wildcard instantiations o raw types o 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 argument o 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 expression o as component type of an array
  24. 24. References ● Q/JavaGenericsFAQ.html