Andrew McDonough

Please note: This page is imported from my wiki, which hasn't been updated in over 10 years. Some of the formatting was lost during the import. I'll try to get around to fixing it someday.

Generics

  • Generics are a feature of a programming language that allows one variable to take different types depending on its context. (?)
  • This is most useful when dealing with collections
  • In C++ these are implemented using templates

Generics in Java

Consider pulling an element out of a list in Java. When the next() method is called, the object needs to be cast to the appropriate type. If the type is not correct, this will not usually be realised until runtime. Generics in Java allow us to tell the compiler what type of object to expect, catching type mismatch errors at compile time.

The implementation of Generics in Java is a highly contovertial topic. Generics are implemented only at high level Java source and not in the JVM byte code. Generics are converted to non generics by the compiler.

  • Reflection is not accurate

Useful Links


Andrew McDonough

Andrew McDonough is a consultant CTO and software developer, currently based between Berlin and London.

Follow Andrew on Twitter or Connect on LinkedIn