In this write-up, we will learn different aspects of Java annotations, and to do so, we have to understand the following concepts:
- What does @ mean in Java?
- What does @ do in Java?
- Difference between Annotations and Comments
- Standard Annotations in Java
- Meta-Annotations in Java
- Custom/User-defined Annotations in Java
- How to Use @ sign in Java
So let’s start!
What does @ mean in Java?
In java, the @ sign allows us to create or use an annotation. Every annotation (i.e. built-in as well as customized) in java starts with the @ sign. So all in all we can say that @ sign is used to provide metadata about the program/source code and it doesn’t affect the execution of code directly.
What does @ do in Java?
When we attach @ symbol to any part of the program then the remaining parts of the program test whether any part of the program has an annotation attached with it or not.
If the program has an annotation then the attached information can be utilized by the remaining parts of the program to work accordingly.
What is the Difference between Annotations and Comments
Now, you must be wondering what the difference is between the java annotations and java comments. Well! The java annotations provide detailed/additional information to the compiler, on the other hand, the comments provide convenience to the programmers in terms of code structure.
Standard Annotations in Java
Standard annotations are also known as predefined or built-in annotations. In java, there are numerous standard annotations and among them, some are used by the java compiler while some annotations can be applied to other annotations (meta-annotations).
The predefined annotations that are used by the java compiler are listed below:
- @Override
- @SuppressWarnings
- @Deprecated
- @FunctionalInterface
- @SafeVarargs
Meta-annotations
The annotations that are used in some other annotations are known as meta-annotations and are listed below:
- @Documented
- @Inherited
- @Retention
- @Target
- @Repeatable
Custom/User-defined Annotations in Java
As the name itself suggests these types of annotations can be created/customized by the user and to do so @interface element will be followed by the annotation name.
If a custom annotation has no value then it is referred as marker annotation, if it has one value in it then it is referred as single value annotation and if it has more than one value then it is referred as the multi-value annotation.
Syntax
The basic syntax of the customized annotations is shown in the following snippet:
Let’s move one step further to understand how to use annotations in java.
How to Use @ sign in Java
Let’s consider the below example for a profound understanding of how to use annotations in java.
Example
In this example we will utilize one of the predefined annotations named @override that specifies the child class is overriding the method of the parent class.
int age = 25;
String name = "Joe";
public void show() {
System.out.println("Employee Name: " + name);
System.out.println("Employee Age: " + age);
}
}
public class AnnotationsExample extends PersonInfo {
int id = 12;
@Override
public void show() {
System.out.println("Employee id: " + id);
}
public static void main(String[] args) {
AnnotationsExample obj = new AnnotationsExample();
obj.show();
}
}
Here in this example we override the show() method of PersonInfo class in the AnnotationExample class. And within the child class we utilize the @Override annotation which tells the java compiler that the show() method is overridden from the parent class:
This is how we can use @ symbol in java.
Conclusion
The @ sign in java is used to represent java annotations. In java, the annotations are a special type of comments that are used to embed some additional information for the Java compiler. Java annotations can be either standard, customized, or meta-annotations. The standard annotations are predefined annotations, customized annotations can be customized by the users, and meta-annotations can be used by other annotations. Annotations can be embedded with the classes, interfaces, constructors, etc.
This write-up provides a comprehensive overview of what does @ means, what it does, and how to use it in java.