JDK annotations

There are other annotations defined in the JDK in addition to those that are to be used to define annotations. We have already seen some of these. The most frequently used is the @Override annotation. When the compiler sees this annotation, it checks that the method really overrides some inherited method. Failing to do so will result in an error, saving us from miserable runtime debugging.

The @Deprecated annotation signals in the documentation of a method, class, or some other element that the element is not to be used. It is still there in the code, because some users may still use it, but in the case of a new development that depends on the library containing the element, the newly developed code should not use it. The annotation has two parameters. One parameter is since, which can have a string value and may deliver version information about how long or since which version of the method, or class is deprecated. The other parameter is forRemoval, which should be true if the element will not appear in the future versions of the library. Some methods may be deprecated because there are better alternatives but the developers do not intend to remove the method from the library. In such a case, the forRemoval can be set to false.

The @SuppressWarning annotation is also a frequently used one, though its use is questionable. It can be used to suppress some of the warnings of the compiler. It is recommended to write code, if possible, which can be compiled without any warning.

The @FunctionalInterface annotation declares that an interface intends to have only one method. Such interfaces can be implemented as lambda expressions. You will learn about lambda expressions later in this chapter. When this annotation is applied on an interface and there is more than one method declared in the interface, the compiler will signal compilation error. This will prevent any developer early on from adding another method to an interface intended to be used together with functional programming and lambda expressions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.119.106