• 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

Why Generics are compile time ?

 
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the interviewer asked me this question about Generics that Why they are made compile time validations ?

Any thought on this ? I have discussed with my friend that possible reason can be "It is runtime(which will be then part of JRE validations) then legacy code has to be changed".
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct. Java has to work with code that was written before generics existed. There's lots of code like that!
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick response Jeanne
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what did you say at the time of the interview?
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to explain similar things , but i was thinking if there is any other disadvantage of it.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Being pedantic here, generic type arguments would probably be validated at compile time regardless of legacy code or type erasure, because that's exactly what generics are for! Validating at compile time is always better than at runtime.
 
Greenhorn
Posts: 14
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics are not only compile time ... for anonymous classes the type information is preserved ... meaning e.g. if you instantiate like
that



you can regain the type information Animal by a complex reflection approach. For all other classes, this does not work.
The instance has to be instantiated by <Animal> and not by <>.

So it is not strictly true that Generics are just compile time ... not important for an exam but still interesting ...

have fun and kind regards,
Stephan
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not just anonymous classes. The key is that the class itself has no generic types left. If that is true, then the generic types of any class higher in the hierarchy is known (always the same) and available through reflection.
 
Stephan Strauss
Greenhorn
Posts: 14
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Not just anonymous classes. The key is that the class itself has no generic types left. If that is true, then the generic types of any class higher in the hierarchy is known (always the same) and available through reflection.



Rob, this is quite interesting ... could you explain a little bit - would be great. thanks.
stephan
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's take this simple example:
You can now get the Integer part through reflection:

This is mostly used in frameworks. I think that JAXB uses this for XmlAdapter classes, to get the value type and bound type.
 
reply
    Bookmark Topic Watch Topic
  • New Topic