Avoid raw types

Raw types refer to using a generic type without specifying a type parameter. For example, List is a raw type, while List<String> is a parameterized type.

When generics were introduced in JDK 1.5, raw types were retained only to maintain backwards compatibility with older versions of Java. Although using raw types is still possible, they should be avoided:


import java.util.*;

public final class AvoidRawTypes {

  void withRawType(){
    //Raw List doesn't self-document, 
    //doesn't state explicitly what it can contain
    List stars = Arrays.asList("Arcturus", "Vega", "Altair");
    Iterator iter = stars.iterator();
    while(iter.hasNext()) {
      String star = (String) iter.next(); //cast needed
  void withParameterizedType(){
    List<String> stars = Arrays.asList("Spica", "Regulus", "Antares");
    for(String star : stars){
  private void log(Object aMessage) {

See Also :
Modernize old code