9.Java Annotations (Annotation)

Source: Internet
Author: User
Tags deprecated modifier

Tag: The method of the element System tool class represents the use of sans types family

I. Three system built-in standard annotations

[Email protected] is a markup annotation type that is used as a labeling method. It illustrates the way that the annotated method overloads the parent class and plays the role of the assertion. If we use this annotation in a method that does not overwrite the parent class method, the Java compiler will alert you with a compilation error. This annotaton often works when we try to override the parent method and then write the wrong method name. The method of use is extremely simple: when using this annotation, just precede the modified method with @override.

2.@Deprecated, the token is obsolete:

The same deprecated is also a marker annotation. When a type or type member uses the @deprecated modifier, the compiler will not encourage the use of this annotated program element. And this modification has a certain "continuity": if we use this obsolete type or member in the code by inheriting or overwriting it, although the inherited or overwritten type or member is not declared as @Deprecated, the compiler still has to call the police.

It is worth noting that this tag is different @Deprecated this annotation type and the @deprecated in Javadoc: The former is recognized by the Java compiler, The latter is a description that is used by the Javadoc tool to generate documents that contain why a program member is obsolete, how it should be banned or substituted.

The Java5.0,java compiler still looks for @deprecated this Javadoc tag as it did in previous versions, and uses them to generate warning messages. But this situation will change in subsequent versions, and we should now start using @deprecated to decorate obsolete methods instead of @deprecated Javadoc tag.

3.suppresswarnnings, suppress compiler warning:

@SuppressWarnings is used to have a selective shutdown compiler warning for classes, methods, member variables, and variable initialization. The Javac compiler provided by Java5.0,sun gives us the-xlint option to warn the compiler of legitimate program code, which in some way represents a program error. For example, when we use a generic collection class without providing its type, the compiler will prompt the warning "unchecked warning". Usually when this happens, we need to find the code that caused the warning. If it does indicate an error, we need to correct it. For example, if the warning message indicates that the switch statement in our code does not overwrite all possible case, then we should add a default instance to avoid this warning.
Sometimes we cannot avoid this warning, for example, we cannot avoid this unchecked warning when we use generic collection classes that must interact with non-generic old code. At this point the @suppresswarning will come in handy, add the @suppresswarnings modifier before the calling method, and tell the compiler to stop warning about this method.
Suppresswarning is not a markup annotation. It has a member of type string[], and the value of this member is the forbidden warning name. For the Javac compiler, the warning name that is valid by the-xlint option is also valid for @suppresswarings, and the compiler ignores the unrecognized warning name. @SuppressWarnings (value={"Rawtypes", "Unchecked"})

@SuppressWarnings ({"Rawtypes", "Unchecked"})

@SuppressWarnings ("unused")

9.Java Annotations (Annotation)

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.