[JAVA] What is an annotation?
Introduction
This time about Java annotation.
It's just a memo that you can easily organize the meaning and merits of annotations that you seem to know but don't know (?).
What is an annotation?
- Annotation = Annotation.
- Write using "@" in the code.
- For example, the code looks like this:
package hoge.nwobhm;
public class Metallica {
@Override
public String playMetal() {
return "riff";
}
public static void main(String[] args) {
Metallica metal= new Metallica ();
System.out.println(metal.playMetal());
}
}
For example, what is there?
Specifically, there are the following.
- Override: Declare that it is an override method.
- Deprecated: Deprecate classes and methods.
- SuppressWarnings: Suppresses warnings at compile time.
In addition, annotations are divided into three types.
-
- Marker: Name only, no data
- Single annotation: One that has one data
-
- Full annotation: Those with multiple data
Can I use only the fixed ones?
- Annotations can be implemented independently.
- Specify "@interface" to implement.
- For example, like this.
package hoge.nwobhm;
public @interface Megadeth {
String value();
}
What are the benefits of using it?
- Annotations are to be compiled, so the code can be unified. (The code is easier to see)
- Annotations can be implemented independently, so they can be used in a wide range of ways.
reference