Fast color source Building steps Java developers should know 5 annotations

Source: Internet
Author: User
Tags object object

Since the launch of JDK5, annotations have become an integral part of the Java ecosystem. While developers have developed countless custom annotations for the Java framework (for example, Spring @autowired), some annotations that the compiler endorses are important.

In this article, we will see the annotations supported by the 5 Java compilers and understand the intended use of the bbs.yasewl.com-speed source building. By the way, we will explore the fundamentals behind its creation, some of the qualities surrounding its use, and some examples of proper application. While some of these annotations are more common than other annotations, Java developers who are not beginners should digest each annotation.

First, we'll delve into one of the most commonly used annotations in Java: @Override.
@Override

The implementation of an overriding method, or the ability to provide implementation for an abstract method, is the core of any object-oriented (OO) language. Because Java is an OO language with many common object-oriented abstractions, any method in a non-final method or interface defined in a non-ultimate superclass (The interface method cannot be final) can be overridden by a quilt class. Although the Overwrite method at the beginning looks simple, many minor bugs may be introduced if executed incorrectly. For example, overriding the Object#equals method with a single parameter overriding the class type is a common error:

public class Foo {
public boolean equals (foo foo) {
Check If the supplied object is equal to this object
}
}
Since all classes are implicitly inherited from the object class, the purpose of the Foo class is to overwrite the Object#equals method, so foo can be tested for equality with any other object in Java. While our intentions are correct, our implementation is not. In fact, our implementations do not overwrite the Object#equals method at all. Instead, we provide an overload of the method: instead of replacing the implementation of the Equals method provided by the object class, we provide a second method to specifically accept the Foo object instead of the object object. Our error can be illustrated with a simple implementation, which returns true for all equality checks, but is never called when the provided object is treated as a (for example, in the Java Collections Framework, JCF).

public class Foo {
public boolean equals (foo foo) {
return true;
}
}
Object foo = new Foo ();
Object Identicalfoo = new Foo ();
System.out.println (Foo.equals (Identicalfoo)); False
This is a very subtle but common error that can be captured by the compiler. Our intention is to overwrite the Object#equals method, but since we have specified a parameter of type Foo instead of object type, we actually provide an overloaded Object#equals method instead of overwriting it. To catch this error, we introduce the @override annotation, which instructs the compiler to check whether the overlay actually has no execution. If a valid overwrite is not performed, an error is thrown. Therefore, we can update the Foo class as follows:

public class Foo {br/> @Override

return true;
}
}
If we try to compile this class, we now receive the following error:

$ Javac Foo.java
Foo.java:3: Error:method does not override or implement a method from a supertypebr/> @Override

1 Error
In essence, we have transformed this implicit assumption of our already covered methods into explicit validation by the compiler. If our intentions are incorrectly implemented, then the Java compiler will issue an error-the code that we did not implement correctly is not allowed to compile successfully. Typically, if any of the following conditions are not met, the Java compiler will issue an error for methods that use @override annotations

Fast color source Building steps Java developers should know 5 annotations

Related Article

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.