What are the 4 built in annotations in Java?

Stuart 63 Published: 07/09/2024

What are the 4 built in annotations in Java?

I'm happy to respond in English for once!

In Java, there are four built-in annotations that serve different purposes:

@Override: This annotation is used to indicate that a method overrides another method in its superclass or interface. It's used to ensure that the method being overridden actually exists and is correctly overridden. If the method doesn't exist or is not overridden correctly, the compiler will throw an error.

For example:

public class Animal {

public void sound() {

System.out.println("The animal makes a sound.");

}

}

public class Dog extends Animal {

@Override

public void sound() {

System.out.println("The dog barks.");

}

}

@Deprecated: This annotation is used to mark a method, field, or class as deprecated, meaning it's considered obsolete and should be avoided. This annotation helps developers know which parts of the code are no longer recommended for use.

For example:

public class Calculator {

public void calculate(int x) {

// This method does nothing

}

@Deprecated

public int multiply(int x, int y) {

return x * y;

}

}

@SuppressWarnings: This annotation is used to suppress specific compiler warnings for a particular piece of code. It's useful when working with legacy code or third-party libraries that produce unnecessary warnings.

For example:

@SuppressWarnings("unchecked")

public void processList(List<?> list) {

// Code here

}

@SuppressWarnings also has another use case: it can suppress all unchecked operations in a method, allowing you to write generic code without worrying about compiler warnings.

For example:

@SuppressWarnings("rawtypes")

public void processMap(Map map) {

// Code here

}

These four built-in annotations help developers maintain high-quality code by ensuring correct method overriding, avoiding deprecated methods, suppressing unnecessary compiler warnings, and writing generic code without worrying about unchecked operations.

java method annotation example

Here's an example of a Java method with annotations:

/**

This is the main method for our program. @author John Doe

@since 1.0

*/

public class Main {

/**

The main method that gets called when you run your program from the command line. @param args Command-line arguments passed to the main method.

*/

public static void main(String[] args) {

System.out.println("Hello, World!");

}

}

Annotations in Java are a way to add extra information about the code, such as what it does, how it should be used, or what exceptions it might throw. These annotations can then be used by tools and other programs that work with your code.

Here's a breakdown of the different kinds of annotations:

@author and @since: These are comments about who wrote the code and when it was written. They're not actually doing anything, but they can give you some context about the code. @param and @return: These tell us what parameters the method takes in, and what it returns (or what exceptions it might throw). For example:
/**

This method prints out the sum of two numbers.





@param first The first number to add together.


@param second The second number to add together.


@return The sum of the two numbers.

*/

public static int add(int first, int second) {

return first + second;

}

In this example, we're telling the world that our add method takes in two integers, first and second, adds them together, and returns the result.

@throws: This tells us what exceptions a method might throw. For example:
/**

This method tries to divide one number by another.





@param dividend The number we're dividing from.


@param divisor The number we're dividing by.


@return The result of the division, or zero if there's an error.


@throws ArithmeticException If there is no common divisor between the two numbers.

*/

public static int divide(int dividend, int divisor) {

try {

return dividend / divisor;

} catch (ArithmeticException e) {

throw new RuntimeException(e);

}

}

In this example, we're telling people that our divide method might throw an ArithmeticException if there's no common divisor between the two numbers.