Using JUNIT 5

Think of annotations as =[Comments, Program Execution Directives]

Annotations are used to provide supplemental information about a program.
Annotations start with ‘@’.
Annotations do not change the action of a compiled program.
Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.
Annotations are not pure comments as they can change the way a program is treated by the compiler.

Hierarchy of Annotations in Java

Note: This program throws a compiler error because we have mentioned override, but not overridden, we have overloaded display.


// Java Program to Demonstrate that Annotations// are Not Barely Comments

// Class 1class Base { // Method public void display() { System.out.println("Base display()"); }}

// Class 2// Main classclass Derived extends Base { // Overriding method as already up in above class @Override public void display(int x) { // Print statement when this method is called System.out.println("Derived display(int )"); }

// Method 2 // Main driver method public static void main(String args[]) { // Creating object of this class inside main() Derived obj = new Derived();

// Calling display() method inside main() obj.display(); }}
10: error: method does not override or implement a method from a supertype
If we remove parameter (int x) or we remove @override, the program compiles fine.

Categories of Annotations

There are broadly 5 categories of annotations as listed:

Marker Annotations
Single value Annotations
Full Annotations
Type Annotations
Repeating Annotations

Category 1: Marker Annotations

Used to mark a declaration. These annotations contain no members and do not consist of any data. Thus, its presence as an annotation is sufficient. Since the marker interface contains no members, simply determining whether it is present or absent is sufficient. @Override is an example of Marker Annotation.

Category 2: Single value Annotations

These annotations contain only one member and allow a shorthand form of specifying the value of the member.
We only need to specify the value for that member when the annotation is applied and don’t need to specify the name of the member. However, in order to use this shorthand, the name of the member must be a value.

Category 3: Full Annotations

These annotations consist of multiple data members, names, values, pairs.
@TestAnnotation(name=”Peanut”, breed=”Bichon”)

Category 4: Type Annotations

These annotations can be applied to any place where a type is being used. For example, we can annotate the return type of a method. These are declared annotated with @Target annotation.
// Java Program to Demonstrate Type Annotation

// Importing required classesimport java.lang.annotation.ElementType;import java.lang.annotation.Target;

// Using target annotation to annotate a type@Target(ElementType.TYPE_USE)

// Declaring a simple type annotation@interface TypeAnnoDemo{}

// Main classpublic class GFG { // Main driver method public static void main(String[] args) {

// Annotating the type of a string @TypeAnnoDemo String string = "I am annotated with a type annotation"; System.out.println(string); abc(); }

// Annotating return type of a function static @TypeAnnoDemo int abc() { System.out.println("This function's return type is annotated"); return 0; }}
I am annotated with a type annotationThis function's return type is annotated

Category 5: Repeating Annotations

These are the annotations that can be applied to a single item more than once. For an annotation to be repeatable it must be annotated with the @Repeatable annotation, which is defined in the java.lang.annotation package. Its value field specifies the container type for the repeatable annotation. The container is specified as an annotation whose value field is an array of the repeatable annotation type.
To create a repeatable annotation, first: the container annotation is created, and then the annotation type is specified as an argument to the @Repeatable annotation.
// Java Program to Demonstrate a Repeatable Annotation

// Importing required classesimport java.lang.annotation.Annotation;import java.lang.annotation.Repeatable;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.reflect.Method;

// Make Words annotation repeatable@Retention(RetentionPolicy.RUNTIME)@Repeatable(MyRepeatedAnnos.class)@interface Words{ String word() default "Hello"; int value() default 0;}

// Create container annotation@Retention(RetentionPolicy.RUNTIME)@interface MyRepeatedAnnos{ Words[] value();}public class Main {

// Repeat Words on newMethod @Words(word = "First", value = 1) @Words(word = "Second", value = 2) public static void newMethod() { Main obj = new Main();

try { Class<?> c = obj.getClass();

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.