@SuppressWarnings (unchecked) function explanation

Source: Internet
Author: User

Explanation One:

Block some compile-time warning messages
The compiler gives a warning when a type conversion is coerced
Plus

Program code

@SuppressWarnings ("Unchecked")

You won't be warned.

EXPLANATION Two:

Note Type

When your code may have a warning, such as a security warning, you can use it to eliminate

This is described in the API

Indicates that the specified compiler warning should be deselected in the annotation element (and in all program elements contained in the annotation element). Note that the set of warnings that are deselected in a given element is a superset of all warnings that are deselected in the containing element. For example, if you comment on a class to suppress a warning and comment on a method to suppress another warning, both warnings will be deselected in this method.

Depending on the style, the programmer should always use this comment on the innermost nested element, where the use is valid. If you want to suppress a warning in a particular method, you should comment on the method instead of commenting on its class.

EXPLANATION Three:

@SuppressWarnings

The last annotation provided by J2SE is @SuppressWarnings. The purpose of this annotation is to give the compiler a directive that tells it to remain silent about some of the warnings inside the code element being annotated.

A little background: J2SE 5.0 adds several new features to the Java language and adds many new warnings along with them and promises to add more warnings in the future. You can increase the-xlint parameter for "Javac" to control whether these warnings are reported (as shown in the @Deprecated section above).

By default, the Sun compiler outputs a warning in a simple two-line format. By adding-xlint:keyword tags (such as-xlint:finally), you can get a complete description of the keyword type error. You can suppress the warning by adding a dash before the keyword, written as-xlint:-keyword. (The complete list of keywords supported by-xlint can be found on the.) The following is a checklist:

The

Keyword

Purpose

Deprecation

Warning when using a class or method that is deprecated

Unchecked

Performs an unchecked conversion warning, such as when using the collection without generics (generics)   to specify the type of collection to save.

Fallthrough

When  Switch  blocks direct access to the next situation without  Break  warning.

Path

Warning when there is a nonexistent path in the classpath, source file path, and so on.

Serial

When a  serialVersionUID  definition warning is missing on a serializable class.

Finally

Any warnings when the  finally  clause does not complete properly.

All

Warnings about all of these conditions.

@SuppressWarnings annotations allow you to selectively suppress warnings in specific snippets (that is, classes or methods). The idea is that when you see a warning, you'll investigate it, and if you're sure it's not an issue, you can add a @SuppressWarnings comment so you don't see the warning again. While it may sound like it will block potential errors, it will actually improve code security because it will prevent you from being indifferent to warnings-every warning you see will be worth noting.

Here is an example of using the @SuppressWarnings to cancel the deprecation warning:

public class DeprecatedExample2 {@Deprecated public static void Foo () {}} public class DeprecatedUser2 {@SuppressWarni NGS (value={"deprecation"}) public static void main (string[] args) {Deprecatedexample2.foo ();}}

@SuppressWarnings annotation receives a "value" variable, which is an array of strings that indicates the warning that will be canceled. The collection of legitimate strings varies with the compiler, but on the JDK, the same set of keywords that can be passed to-xlint (very handy). and ask the compiler to ignore any keywords they don't recognize, which is handy when you're using a few different compilers.

Because the @SuppressWarnings annotation only receives one parameter and uses a special name of "value" for the parameter, you can choose to omit value= as a handy abbreviation:

public class DeprecatedUser2 {@SuppressWarnings ({' deprecation '}) public static void main (string[] args) {Deprecatedexam Ple2.foo (); } }

You can pass any number of string values in a single array parameter to the annotation and place the annotation at any level. For example, the following example code indicates that the deprecation warning for the entire class will be canceled, and only the unchecked and Fallthrough warnings are canceled within the main () method code:

Import java.util.*; @SuppressWarnings ({"Deprecation"}) public class Nongenerics {@SuppressWarnings ({"Unchecked", "Fallthrough"}) public static void Main (string[] args) {runtime.runfinalizersonexit (); List List = new ArrayList (); List.add ("foo"); public static void Foo () {List List = new ArrayList (); List.add ("foo");}}

is @SuppressWarnings more useful than the first two annotations? That's absolutely true. However, this annotation is not fully supported in JDK 1.5.0, and if you try it with 1.5.%, then it will resemble a no-action instruction. Calling-xlint:-deprecation also has no effect. Sun does not state when it will add support, but it implies that it will be implemented in a dot version that is about to be launched.

Further

If you try to view these properties in the Javadocs page, you may find it difficult to locate them. They are located in the core Java.lang package, but are somewhat hidden, and they appear at the bottom of the Javadoc class, listed behind Exceptions and Errors.

Have you noticed the unfamiliar annotations @Target and @Retention appended to the suppresswarnings annotation? These are called metadata annotations, and they describe where the annotation applies. I'll cover them in the second article in this series and explain how to apply metadata annotations to your own annotations.

@SuppressWarnings (unchecked) function explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.