Introduction

In this article, we discuss Annotation in Java.

What is Annotation in Java?

Annotation is a tag that represents the metadata (data about data). It is attached to a class, interface, method or field to indicate some additional information that can be used by the Java compiler and JVM.
Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

Some uses of Annotations

  • Runtime processing
  • At runtime, some annotations are available to be examined.
  • Provide information for the compiler.
  • It can be used by the compiler to detect errors or suppress warnings.
  • Deployment-time and Compile-time processing.
  • Software tools can process annotation information to generate code, XML files, and so forth.

Built-In-Annotations

Several built-in annotations are available in Java. Some annotations are applied to Java code and some to other annotations.

Built-In-Annotation that are applied in Java code

Built-In Annotations that are applied in other annotations

Understanding Built-In Annotations that are applied in Java code
Let's understand Built-In annotation first.

@override

  • This annotation provides assurance about the overriding, in other words, that the subclass overrides the parent correctly.
  • If it does not override correctly then it produces a compile-time error.
  • Sometimes, due to some silly mistakes like spelling mistakes etcetera, an error is generated. So to avoid the error use @Override annotation.
Example
  1. class Parent {
  2. void doSomework() {
  3. System.out.println("Do some work");
  4. }
  5. }
  6. class Children extends Parent {
  7. @Override
  8. void doSomework() {
  9. System.out.println("No work should be done");
  10. }
  11. }
  12. class Check {
  13. public static void main(String args[]) {
  14. Parent prnt = new Children();
  15. prnt.doSomework();
  16. }
  17. }
Output
Fig-1.jpg

@SuppressWarnings

This annotation is used to suppress warnings issued by the compiler.
Example
  1. import java.util.*;
  2. class Test {
  3. @SuppressWarnings("unchecked")
  4. public static void main(String[] args) {
  5. ArrayList arylst = new ArrayList();
  6. arylst.add("Jaun");
  7. arylst.add("Paul");
  8. arylst.add("San");
  9. for (Object o: arylst)
  10. System.out.println(o);
  11. }
  12. }
Output
Fig-2.jpg

@Deprecated

@Deprecated marks methods as deprecated, so the compiler understands it and produces a warning and it shows a message to us that it will be removed in the future. So it is not a good approach to use such type of methods.
Example
  1. class Main {
  2. void main() {
  3. System.out.println("hello main");
  4. }
  5. @Deprecated
  6. void child() {
  7. System.out.println("hello Child");
  8. }
  9. }
  10. class Test1 {
  11. public static void main(String args[]) {
  12. Main mn = new Main();
  13. mn.child();
  14. }
  15. }
Output
At compile-Time
It shows an error at compile-time as shown below:
fig-3.jpg
At Run-Time
But at run-time it runs well, as in the following:
Fig-4.jpg