Annotation Types in Java

Types of Annotation in Java

In order to add or add-on program information we use Annotations Types in Java. The language uses annotations to provide more information about the program.

These annotations are the tags that are attached to classes, interfaces, methods, and fields to designate extra information that the Java compiler and Java Virtual Machine may use. It is an alternative to XML (Extensible Markup Language ) and Java marker interfaces since Annotations types in Java are used to offer more information.

Annotation Types in Java

Annotations in Java

Annotations can be used to add extra details about a program.

  • It begins with – “@.”
  • They do not affect how a built program behaves.
  • Instance variables, constructors, methods, classes, and other program elements can all be annotated to add metadata (information).
  • As they can alter how a program is handled by the compiler, annotations are not simply comments. See the code example below.
  • An alternative to XML and Java marker interfaces, annotations are essentially used to provide extra information.

Predefined Types of Java Annotations

There are seven built-in annotations in Java.

  • The four imported from java.lang.annotation

@Retention, @Documented, @Target, and @Inherited

Types of Annotations in Java
  • The three in java.lang

@Deprecated, @Override, and @SuppressWarnings

Example Code for @Deprecated method

Run
import java.util.*;

public class Main{
  // We're in the deprecated method which is replaced by the newMethod()
  @Deprecated
  public static void PrepInsta(){ 
    System.out.println("I'm in Deprecated method of PrepInsta");
  } 

  public static void main(String args[]) {
    PrepInsta();
  }
}
Output:

I'm in Deprecated method of PrepInsta

Categories of Annotations Types in Java

There are generally 5 types of annotations.

What use do Java Annotations serve?

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription