Java 5 (2)

Source: Internet
Author: User

Annotations)

Annotation is a modifier that can be added to the code. It can be used for package declaration, type declaration, constructors, methods, fields, parameters, and variables. Tiger includes built-in comments and supports custom comments compiled by you.

It has three built-in Annotations: suppresswarnings, deprecated, and override.

Suppresswarnings

This annotation prevents warnings thrown by the compiler at the class and method levels. Sometimes you know that it is better to use some methods that are not recommended, or you must execute some static methods that have type security. If you use these methods, you will continuously receive warnings about irrelevant things. Suppresswarnings can help the compiler not bother you!

SupresswarningsYesWith a variable-so you use a single annotation type with the variable. A value array can be used to provide variables. Each value indicates a specific warning type to block.

@ Suppresswarnings ("deprecation") // use the unrecommended public static void selfdestruct () {thread. currentthread (). Stop ();}
   
@ Suppresswarings ("unchecked") Public void nongenericsmethod () {list wordlist = new arraylist (); // The wordlist type of the value in the list is not specified. add ("foo"); // exceptions may be thrown}
The values of variables in suppresswarnings use arrays, allowing you to block multiple warnings in the same comment. For example,
@SuppressWarnings("unchecked", "fallthrough")Use an array of two values. This function is for processing
Errors provide a very flexible approach without a lot of work.
    Deprecated
Deprecated is not very useful. UseDeprecatedTo comment out methods that should not be used. Majority
In this case, @ deprecated and javadoc are both used.
Public class deprecatedclass {@ deprecated public void dosomething () {// code
  }
// Replace dosomething () with the public void dosomethingelse () {// code }}
If the declared method is replaced or called, the compiler processes the annotation and finds that
Method, and send an error message.
    Override
Override should be used only for methods (not for classes, package declarations, or other constructor ). It indicates that the annotation method will overwrite
Class.
@Overridepublic int hashCode() {    ...}
If you write hashcode () as hashcode () "C", there will be no errors during compilation, and only errors will be reported during runtime.
By using override annotations, if you do not actually execute the reload compilation, an error is reported. Modify override comment in the parent class Method
Changes also take effect. For example, if the parent class method adds a parameter or the method is renamed, an error occurs during subclass compilation.
It will not be confusing.

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.