• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic - Type Erasure removes information (at compile time) required at runtime. How does it work?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the follow code for Generic. Because of erasure property I defined the methods differently.



However since the compiler erases the generics these classes will not carry this information forward so does this mean if packaged in a jar I will not be able to call these methods. I know that in real world scenario I am still able to call and get good results but how is "type erasure" effective if this information is deleted during compilation yet when it is required at runtime.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamal Tripathi wrote:how is "type erasure" effective...


Type erasure is effective at allowing us to mix legacy code with generics code, however, due to the design of your code, mixing legacy code with generics code will lead to an 'ambiguity' compiler error.

Kamal Tripathi wrote:...when it is required at runtime


It is not required at runtime. At compile time, the compiler removes all information related to type parameters and type arguments. It is able to do this because this information is not required at runtime. At compile time, the compiler decides which overloaded method will be called.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic