Java @SuppressWarnings annotation examples
- Details
- Written by Nam Ha Minh
- Last Updated on 22 August 2019   |   Print Email
@SuppressWarnings("unchecked") @SuppressWarnings({"unchecked", "deprecation"})List of warnings vary among Java compilers. The Java Language Specification mentions only “unchecked” warning (section 9.6.3.5). To see the list of warnings which would be issued by the Oracle’s Java compiler, type the following command:
javac -X
We’ll see the complete list of warnings after the line starts with -Xlint:{, as shown in the following screenshot:So the warnings issued by Oracle’s Java compiler (as of Java 1.7) are: all, cast, classfile, deprecation, dep-ann, divzero, empty, fallthrough, finally, options, overrides, path, processing, rawtypes, serial, static, try, unchecked, varargs.Different IDEs provide different list of warnings issued by their own compilers, e.g. list of suppress warnings provided by Eclipse IDE’s Java compiler.Here are some of the most commonly used warnings which have same meaning across compilers: all, deprecation, unchecked.Java @SuppressWarnings Examples:
- Suppressing warnings on using unchecked generic types operations:
@SuppressWarnings("unchecked") 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:XYZ.java uses unchecked or unsafe operations.
- Suppressing warnings on using deprecated APIs:
@SuppressWarnings("deprecation") public void showDialog() { JDialog dialog = new JDialog(); dialog.show(); // this is a deprecated method }
- Without the @SuppressWarnings("deprecation") annotation, the compiler will issue this warning:
XYZ.java uses or overrides a deprecated API.
- Suppressing multiple warnings:
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) { @SuppressWarnings("unchecked") List<String> list = (List<String>) inputList; // unsafe cast }
Some Notes about @SuppressWarnings:
- Undefined warnings have no effect, e.g. @SuppressWarnings(“blahblah”). The compiler will ignore that silently.
- When a program’s element is annotated by the @SuppressWarnings, all of its sub elements are also affected. For example, if you suppress a warning at class level, then all code inside that class is also applied.
- It’s recommend to this annotation on the most deeply nested element wherever possible.
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand abstraction in Java
- Understand encapsulation in Java
- Understand inheritance in Java
- Understand polymorphism in Java
Comments
then, square both sides.
: x + 15 = 225 -15x+x
then, collect like terms.
//if everything has been done correctly, you are left with:
17x = 210
finally, divide both sides by 17.
x = 30.
So you register to get the videos here: newsletter.codejava.net