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
Would you use this technique?
Yes   No   Undecided   
© 2018 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -