type Earasures

 


Generics 

      —> Generics help you to generalize your type params.    

                                                                                                           

      —> Generics works based on the concept called Type Erasures.



Erasures :


—>Erasures ensure to Erasure or translate generics to specific types at compile time.


Compiler follows below steps for the erasure.


     —>  Erase the type params.


                   Replace all type parameters in generic types with their unbounded to java.lang.Object 


                     In the below example, Item<T> unbounded , So  ‘ T ‘ will be replaced with Java.lang.Object in the byte code.



    • Snapshot : 1      (Generic)                         Snapshot : 2  (Non-Generic)  




    • .Replace all type parameters in generic types with their bounds .

    •      In this below example Item<Integer> is bounded to Number  (Any class which extends Number class).


    •                                                 Snapshot : 3  (Generic)                                                                                                                                                                 Snapshot : 4 (Non-Generic)







  The type erasure of its leftmost bound, or type Object if no bound was specified.

  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 parameters

type erasure

<T>

Object

<T extends Number>

Number

<T extends Comparable<T>>

Comparable

<T extends Cloneable & Comparable<T>>

Cloneable

<T extends Object & Comparable<T>>

Object

<S, T extends S>

Object,Object



     —>  Add casts if necessary. 


    The implicit casts do not add any overhead because the casts added by the compiler are exactly the casts that would added in non generic code.


     



     —>  Add Bridge methods.


when you write code with generics, it compiles in almost exactly the same way as the code you would have written without generics.

In the case of a parameterized interface or classes such as Item<T>,(Refer snapshot : 1) this may cause additional methods to be inserted by the compiler; these additional methods are called bridges, these are bridge methods are essential to preserve the overriding as expected. 


Snapshot 1:shows the Item class generic code.  the set()  takes an argument of type T and get() returns of type T. 

       Snapshot 3 : In the generic class IntItem, set()  takes an argument Integer and get() returns of type Integer. The bridge method is generated automatically by the                               compiler (Snapshot 4)

Snapshot 2 : shows a simplified version of non generic class Item ,  the java.lang.Object as argument passed to the set() and as a return type for get() .

       Snapshot 4 : In the non generic class IntItem, there are four methods(2 get() and 2 set()). The first set() takes as argument as Integer  and  the get() returns Integer  are obvious                               methods you might expect, The second get() and set() with Object , it casts the object to an integer and invokes the first get() and set with Integer methods. The second         methods are necessary in order to override get() and set() method of Item class.These second methods are called bridge methods.

  Why do the compiler add Bridge methods :

Imagine the below code !!!



  the compiler can not be sure that the “integerItem “variable contains an instance of IntItem  or of Item  Thus, there is no guarantee that a method with the byte code signature set(Integer):void even exists. To solve this problem , Java compiler applies a type erasure and translate Item<Integer> into a raw type. By looking at the raw type, the method set(Object): void always exists and invoked which is defined on Item class itself .

Comments