Tuesday, November 30, 2010

A little insight on Java's 'type erasure' terminology

As everyone knows, generics have been introduced since Java 1.5 to enforce compile-time type-correctness. However, at run-time, all the type safety is removed via a process called 'type erasure'. So all typed Collections will have their type removed from the bytecode at runtime and explicit casts will be introduced when extracting elements from the Collections.  For example List<Double> list = new ArrayList<Double>();  will be translated at runtime to List list = new ArrayList(); . As a result of type erasure, type parameters cannot be determined at runtime. This change has been introduced in ordered to ensure pre-Java 5 code (you may call it 'legacy code') inter-operates peacefully with the newly introduced generics.

No comments: