Friday, March 18, 2011

Java Trivia - Take 1

True or false?
It is preferable to write
List dogs = new ArrayList();
instead of
List<Dog> dogs = new ArrayList<Dog>();
because raw types enable developers to avoid needless verbiage.

Answer: False.
While this may seem like needless verbiage, it's not, as Joshua Bloch argues: "By telling the compiler what kind of elements the list contains, you enable it to find many errors at compile time that would otherwise cause a ClassCastException at runtime. You also eliminate ugly casts from your program."

No comments:

Post a Comment