What are the 4 built in annotations in Java?

Harry 22 Published: 08/16/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 annotation example w3schools

Here's an example of using annotations in Java with W3Schools:

Java is a statically-typed language that uses annotations to provide additional information about the code. Annotations are used to define metadata about the class, method, or field.

In this example, we will use Java annotation @Override to override a method from its superclass.

Example:

// Superclass

public class Animal {

public void sound() {

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

}

}

// Subclass

public class Dog extends Animal {

// Overriding the sound() method

@Override

public void sound() {

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

}

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.sound(); // Output: The dog barks

}

}

Output:

The dog barks

As you can see, the @Override annotation is used to indicate that the sound() method in the Dog class is overriding a method from its superclass (Animal). This is useful for catching any mistakes if you forget to override a method or if you accidentally create a new method with the same name as one in the superclass.

More Java Annotations:

@Deprecated: Indicates that a method, field, or class is deprecated and should not be used. @SuppressWarnings: Suppresses specific compiler warnings for a given scope (e.g., a class, method, or block of code). @Target: Specifies the level at which an annotation applies (e.g., type, method, constructor, etc.). @Retention: Specifies how long an annotation is retained (e.g., at compile-time, runtime, etc.). @Repeatable: Allows an annotation to be repeated multiple times on a single declaration.

W3Schools Java Annotation Tutorial:

Here's a tutorial on using annotations in Java provided by W3Schools:

Java Annotations

This tutorial provides more examples and explanations of how to use various Java annotations. It also covers the different types of annotations, including those for classes, methods, fields, and packages.

Conclusion:

Annotations are a powerful tool in Java that allow you to add metadata to your code. By using annotations like @Override, you can make your code more readable and maintainable by explicitly declaring what parts of your code are overriding or implementing certain behaviors. Annotations can also help you catch errors and improve the overall quality of your code.

I hope this helps! Let me know if you have any questions or need further clarification on how to use Java annotations.