Common built-in annotations in java

Common built-in annotations in java

The Java programming language provides Java developers with several built-in @Override, @Deprecated, @SuppressWarnings, and @FunctionalInterface annotation types as ready-made selection choices. These provide the Java compiler, JVM, or Java development tools with important detailed information when needed. Annotations help quickly improve the accuracy, readability, and maintainability of your program code.

Common built-in annotations in java

The @Override annotation type in Java.

The @Override annotation type is used in Java programs when a user-defined subclass overrides a method from its parent class or implements a method from a class interface.

Example of the @Override annotation.

class Course {

void selection() {

System.out.println(“Desire Course selection”);

}

}

class Olevel extends Course {

@Override

void selection() {

System.out.println(“O level course selection”);

}

}

public class Main {

public static void main(String[] args) {

Course c = new Course();

c.selection();

Olevel o = new Olevel();

o.selection();

// Here we define runtime polymorphism

Course obj = new Olevel();

obj.selection();

}

}

The purpose of the @Override Java annotation method here is to help the compiler detect incorrect method overriding.

@Deprecated annotation type in Java.

The @Deprecated annotation type method is used in Java programs when a user-defined custom class, method, or field should no longer be used in the program.

Example of the @Deprecated annotation.

class Gadgets {

@Deprecated

void laptopMethod() {

System.out.println(“laptop method”);

}

void updateMethod() {

System.out.println(“Update MacBook Pro M5 method”);

}

}

public class Main {

public static void main(String[] args) {

Gadgets obj = new Gadgets();

obj.laptopMethod(); // Here we use the Deprecated annotation method

obj.updateMethod();

}

}

Using laptopMethod() in this example may result in a compiler warning error.

The purpose of the @Deprecated annotation method in a Java program is to warn Java developers that an API is obsolete and may be removed or replaced in existing programs.

The @SuppressWarnings annotation type in Java.

The @SuppressWarnings annotation type in Java is used when a Java user tells the compiler to suppress specific warnings in a program.

@SuppressWarnings(“unchecked”)

void test() {

// User-defined Suppress Warnings Java code

}

Another example of @SuppressWarnings in Java.

class Gadgets {

@Deprecated

void laptopMethod() {

System.out.println(“Laptop Method Define”);

}

}

public class Main {

@SuppressWarnings(“deprecation”)

void sample() {

Gadgets obj = new Gadgets();

obj.laptopMethod();

}

public static void main(String[] args) {

Main t = new Main();

t.sample();

}

}

In this example, the @SuppressWarnings annotation method prevents known, intentionally generated compiler warnings from causing any errors in the output.

@FunctionalInterface annotation type in Java.

The @FunctionalInterface annotation type method is used in Java programs when a class interface is intended to be a functional interface.

A functional interface defines only one abstract method.

Example of the @FunctionalInterface annotation.

@FunctionalInterface

interface Total_value {

int total(int p, int q);

}

public class Main {

public static void main(String[] args) {

// Here we are using a Lambda Expression

Total_value t = (p, q) -> p + q;

System.out.println(“The total of -: ” + t.total(3, 4));

}

}

Here’s its output.

7

If another class abstract method is added to this, the Java compiler displays an error report.

@SafeVarargs annotation type in Java.

The @SafeVarargs annotation type is used in Java programs when methods or constructors with variable-length generic arguments are used to indicate that the implementation does not perform unsafe operations on the varargs variable parameter.

Example of the @SafeVarargs annotation.

public class Main {

@SafeVarargs

static <T> void preview(T… values) {

for (T value : values) {

System.out.println(value);

}

}

public static void main(String[] args) {

preview(7, 1, 9, 3, 8);

preview(“Java”, “Python”, “Rust”, “Go”, “Matlab”);

}

In this example, the purpose of the @SafeVarargs annotation is to suppress some unchecked warnings associated with generic varargs.

@Native annotation type in Java.

The @Native annotation type method is used in a Java program when a class field is a constant method referenced by native code.

Example of the @Native annotation.

import java.lang.annotation.Native;

public class Sample {

@Native

public static final int INTEGER = 7;

public static void main(String[] args) {

System.out.println(“THE INTEGER VALUE IS -: ” + INTEGER);

}

}

Common Java Annotations Method Unique Purpose

#Annotation methodPurpose of each java Annotation
1.@Override AnnotationThis annotation method used to indicate method overriding process and task in active program
2.@Deprecated AnnotationThis annotation method used to mark outdate api in active java program
3.@SuppressWarnings AnnotationThis annotation method used to suppress compiler warnings any message or issues
4.@FunctionalInterface AnnotationThis annotation method used to indicate a functional interface of active method
5.@SafeVarargs AnnotationThis annotation method used to suppress certain generic-varargs warnings or message
6.@Native AnnotationThis annotation method used to indicate a constant used by native code

Conclusion of Common Java Built-in Annotations.

Common annotations in Java are easy to remember.

@Override annotations.

→ It helps to override a method in a Java program.

@Deprecated annotations.

→ It indicates that an old API should not be used in a Java program.

@SuppressWarnings annotations.

→ It helps to ignore any indicated warnings in a Java program.

@FunctionalInterface annotations.

→ It is used with an abstract method in a Java program.

@SafeVarargs annotations.

→ It is used for safe generic varargs in a Java program.

@Native annotations.

→ It is used with a native program source code in a Java program.

Routine is the most popular annotation method used in Java programs. There is @Override, @Deprecated, @SuppressWarnings, and @FunctionalInterface, etc.

Leave a Reply