Real'sHowTo AddThis Feed Button
Custom Search

Eliminate "[unchecked] unchecked call ..." compiler warning Tag(s): Language


import java.util.*;

public class ArrayListGeneric {
  public static void main(String[] args) {
    ArrayList data = new ArrayList();
    data.add("hello");
    data.add("world");


    Iterator<String> it = data.iterator();
    while (it.hasNext()) {
      String s = it.next();
      System.out.println(s);
    }
  }
}
When compiling the above class, the compiler (jdk1.5) emits the following warnings :
C:\ArrayListGeneric.java:21: 
 warning: [unchecked] unchecked call to add(E) as a member of the raw type 
  java.util.ArrayList
    data.add("hello");
            ^
C:\ArrayListGeneric.java:22: 
 warning: [unchecked] unchecked call to add(E) as a member of the raw type 
      java.util.ArrayList
    data.add("world");
            ^
C:\ArrayListGeneric.java:25: warning: [unchecked] unchecked conversion
found   : java.util.Iterator
required: java.util.Iterator
    Iterator it = data.iterator();
                                       ^
3 WARNINGS
Since there are only warnings, your class is ready to run but ... it's not bad idea to eliminate the warnings in production code.

Simply add the expected type (between < and >) after the class.

import java.util.*;

public class ArrayListGeneric {
  public static void main(String[] args) {
    ArrayList<String> data = new ArrayList<String>();
    data.add("hello");
    data.add("world");


    Iterator<String> it = data.iterator();
    while (it.hasNext()) {
      String s = it.next();
      System.out.println(s);
    }
  }
}

In JDK 1.6, it will be possible to insert a special annotation to suppress this kind of warning, something like :

import java.util.*;

public class ArrayListGeneric {

  @SuppressWarnings("unchecked") 
  public static void main(String[] args) {
    ArrayList data = new ArrayList();
    data.add("hello");
    data.add("world");


    Iterator it = data.iterator();
    while (it.hasNext()) {
      String s = it.next();
      System.out.println(s);
    }
  }
}

A complete list of possible @SuppressWarnings parameters can be found at http://mindprod.com/jgloss/annotations.html.

ANT

When compiling with ANT, you see the following output :
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
but it's not easy to see the details of those unsafe operations. To add the -Xlint, you need to use the compilerarg tag in the javac task definition.

Something like

<target name="compile" description="Compile code">
       <mkdir dir="${bin}"/>
       <mkdir dir="${lib}"/>
       <javac srcdir="${src}" destdir="${bin}" 
              includeAntRuntime="no" 
              classpathref="lib.path" 
              debug="${compile.debug}">
           <compilerarg value="-Xlint:unchecked"/>
       </javac>
</target>

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]