Java @SuppressWarnings Annotation

@SuppressWarnings Annotation indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).
The @SuppressWarnings annotation type allows Java programmers to disable compilation warnings for a certain part of a program (type, field, method, parameter, constructor, and local variable). Normally warnings are good. However, in some cases, they would be inappropriate and annoying. So programmers can choose to tell the compiler ignoring such warnings if needed.
When you program with generics, you'll see a lot of compiler warnings:
  • unchecked cast warnings
  • unchecked method invocation warnings
  • unchecked generic array creation warnings
  • unchecked conversion warnings
Eliminate every unchecked warning that you can. If you eliminate all warnings, you are assured that the code is TYPESAFE.

SuppressWarnings Annotation Source Code

public @interface SuppressWarnings {
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
    String[] value();
From the above code, notice that @SuppressWarnings annotation applicable to a field, method, parameter, constructor, and local variable

SuppressWarnings Annotation Examples

Suppressing warnings on using unchecked generic types operations:
void uncheckedGenerics() {
    List words = new ArrayList();
    words.add("hello"); // this causes unchecked warning
If the above code is compiled without @SuppressWarnings("unchecked") annotation, the compiler will complain like this: uses unchecked or unsafe operations.

Suppressing warnings on using deprecated APIs: 
public void showDialog() {
    JDialog dialog = new JDialog();;  // this is a deprecated method
Without the @SuppressWarnings("deprecation") annotation, the compiler will issue this warning: uses or overrides a deprecated API.
Suppress all unchecked and deprecation warnings for all code inside the Foo class below:
@SuppressWarnings({"unchecked", "deprecation"})
class Foo {
    // code that may issue unchecked and deprecation warnings
Suppressing warnings on local variable declaration:
void foo(List inputList) {
    List<String> list = (List<String>) inputList; // unsafe cast