In other words, even though you write
List
By the time the compiler is done with it, the JVM sees what it always saw before Java 5 and generics:
List myList = new ArrayList();
The compiler even inserts the casts for you—the casts you had to do to get things out of a pre-Java 5 collection. Think of generics as strictly a compile-time protection. The compiler uses generic type information (the
public class GenericTest
{
public static void main(String[] args)
{
List
list1.add(7);
list1.add(16);
NonGeneric ng = new NonGeneric ();
ng.insert(list1);
System.out.println(list1);
}
}
class NonGeneric
{
void insert(List list)
{
list.add("hello");
}
}
[7, 16, hello]