Annotation is a metadata. A metadata is a data about the data. It is a sort of an information that is used to inform the compiler that the code below or beside it is used as whatever is the annotation is about. Java has some predefined annotations stored in its library.
Usage of annotations
1. To inform the compiler about warnings or errors
2. To generate code during deployment
3. To examine code during runtime
Syntax:
@Entity
Example:
When you implement an interface's abstract method, it says "@Override" in your class.
==================
import java.lang.annotation.Documented;
@Documented
public @interface DemoRepeat {
Demo1[] value();
}import java.lang.annotation.Documented;
import java.lang.annotation.Repeatable;
@Repeatable(DemoRepeat.class)
@Documented
public @interface Demo1 {
String Hello();
}public class AnnotationTypeDemo {
/**
* @Document was put on top of the annotation
* to make it possible for this
* @Demo1 to be place here in javadoc comment.
* */
@Demo1(Hello = "HELLO WORLD") private final String str1;
@Demo1(Hello = "WELCOME WORLD") private final String str2;
AnnotationTypeDemo(String hello){
str1 = "hello " + hello;
str2 = "Welcome " + hello;
}
public static void main(String[] args){
AnnotationTypeDemo demo = new AnnotationTypeDemo("Java");
System.out.println(demo.str1);
System.out.println(demo.str2);
}
}==================
Results:
hello JavaWelcome Java
Annotation is a form of interface. As you can see on the code above. All i did was to make the annotation acts like a comment. The annotation informs that the string to be used is just for greetings.
Predefined Annotations
1. @Deprecated - this annotation informs that the code below it or beside is obsolete. To use this annotation, you need to include this in the javadoc comment. Javadoc comment is just a comment but just for documentation.
2. @Override- this annotation is informs that one method of the super class will be overridden.
3. @SuppressWarnings - this annotation suppresses the warnings that the compiler will generate in your code.
4. @SafeVarargs - this annotation suppresses the warnings that the compiler will generate in case there is a varargs coded that is unsafe.
5. @FunctionalInterface- you can used this annotation to inform that the interface you coded is a functional interface.
6. meta annotations - this is an annotation applied to other annotations such as document annotation in the code.
Note:
It would be unwise to include all the annotation in here since the significance of it is not that much unless the annotation used was to generate a code or the annotation used was for the test of your program. You can check for other annotation in the oracle website.
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...