Why annotation is required, we can use comment also to give information about data like metadata ?
- If you use comment in class, field, constructor or method level then it can reach max to max source file only, means when compiler compile .java file to .class file. It exclude comment section so it won’t be available.
- Before Java 5 to achieve such kind of functionality used xml based configuration like in hibernate needs a .hbm file or for servlet needs to add configuration in web.xml file.
What is Annotation in java ?
- In the Java computer programming language, an annotation is a form of syntactic metadata (a set of data that describes and gives information about other data) that can be added to Java source code.
- Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc tags, Java annotations can be reflective in that they can be embedded in class files generated by the compiler and may be retained by the Java VM to be made retrievable at run-time. It is possible to create meta-annotations out of the existing ones in Java.
- Annotations are the part of java from Java 5 before that there is not concept like annotations. So we should use xml based configuration.
Type of Annotations
- Marker Annotation – @Override, @WebListener
- Single Valued Annotation – @SuppressedWarning(“unchecked”) or @SuppressedWarning(value = “unchecked”)
- Multi valued Annotation – @WebServlet(name = “login”, urlPatterns = “/login”)
Why are Annotations better then xml ?
XML – Servlet configuration in web.xml before Servlet 2.X like below code
<web-app> <servlet> <servlet-name>helloworld</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/helloworld</url-pattern> </servlet-mapping> </web-app>
Annotation – after Servlet 3.X
@WebServlet(name = “helloworld“, urlPatterns = “/helloworld”)
See the difference so before java 5, need to write a code in xml file.
Ex – Servlet – like above, Hibernate – domain.hbm.xml, Spring – Need to create bean in applicatonContext xml file
XML v/s Annotation
| XML | Annotation |
| up to jdk1.4 | jdk 5.0 |
| JDBC 3.X | JDBC 4.0 |
| Servlet 2.5 | Servlet 3.X |
| Struts 1.X | Struts 2.X |
| JSF 1.X | JSF 2.X |
| Hibernate 3.2.4 | Hibernate 3.2.5 |
| Spring 2.X | Spring 3.X |
| EJB 2.X | EJB 3.X |
General purpose annotations
- @Override – annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.
- @SuppressWarnings – annotation is used to suppress compiler warnings for the annotated element. Specifically, the unchecked category allows suppression of compiler warnings generated as a result of unchecked type casts.
- @Deprecated – Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead. Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used.
- @FunctionalInterface – An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method.
Meta annotations
- @Documented – to ensure that classes using your annotation show this in their generated JavaDoc also.
- @Inherited – It means that subclasses of annotated classes are considered having the same annotation as their superclass.
- @Target – it defined that where you can put your annotation based on type like class, method, constructor, field, localvariable.
- @Retention – based on Retention Policy can use that like Source, Class or at Runtime.
For more detail, Please watch below video –
WOW ! Just plain sentences rather than hefty words.I understood the findamentals just in one read.
Thank you )
LikeLike