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
This compiler warning isn't very descriptive, but the second note suggests that you recompile with -xlint:unchecked. If you do, you'll get something like this:
javac -Xlint:unchecked TestBadLegacy.java
TestBadLegacy.java:17: warning: [unchecked] unchecked call to
add(E) as a member of the raw type java.util.List
list.add(new String("42"));
^
1 warning
When you compile with the -Xlint:unchecked flag, the compiler shows you exactly which method(s) might be doing something dangerous.
No comments:
Post a Comment