Annotation is new feature in java provided by jdk5.0 version. Annotations can be used to describe meta data in java programs. Meta data in the sense data about the data, especially in the java programming data about our coding part is called as meta data. Here to describe some information about our coding part we are going to use annotations. Here one question can arise, to describe data we have comments in our program, so what is the requirement to go for annotations...the answer is very simple. For this we need to identify the compilation structure here, let?s see.
Let we have a .java file. Then we compile to generate .class file(compilation). Generally .class files are completely depend upon how many classes, how many abstract classes, how many enums, how many interfaces, how many inner classes which have written in our java file. For each and every class, for each and every abstract class, for each and every enums, for each and every interfaces, for each and every inner classes, a separate .class file will be created. Here assume it this is a main class.class file. Now let?s focus the compilation process.
Java was initially launched as Java 1.0 but soon after its initial release, Java 1.1 was launched, then released 1.2, 1.3, 1.4 and Java1.5. Java 1.5 was called J2SE 5, it added following major new features, such as Generics, Annotations, Autoboxing and autounboxing, Enumerations, For-each Loop, Varargs, Static Import, Formatted I/O, Concurrency utilities. Then Java SE7 came to the market with some new updates and currently JavaSE8 have released.
The compiler consists under six number of phases like Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Code Optimization, Code Generation. Now to understand our question that what is requirement of annotation over comments is the requirement of functionality of Lexical Analysis phase only. The basic functionality of Lexical Analysis is only two functions : (1) It takes the complete source program and the source program is stored in the form of buffer which is called as buffering. (2)After buffering Lexical Analysis have to read source program and divide into no. of pieces, where each and every piece is called as token. That means this phase will perform the tokenization with respect to program. Then the remaining phases will perform their task accordingly and finally .class file will be generated. In tokenization, whatever the meta data is specified in our source program to describe some information by comments, then the Lexical Analysis phase will remove all the comments from the source programs. It is the functionality of Lexical Analysis phase. So if we open .class file then we may not see the commented information in our source program. But our requirement is different, that is we want to provide description to our program and we want to bring description up to source code(.java file), up to .class file and up to run time application to simplify debugging process and testing process. We want to debug on the basis of meta data and test my application on the basis of meta data. To need to simplification of debugging and testing application we need to bring meta data up to run time our application there. So for this comments are not sufficient. So instead of comment we use annotations. So if we differentiate between comment and annotation, then the difference is comments scope is maximum up to source code but annotations scope is up to source code, up to .class file and up to run time of application. Here we observed that if we want to access meta data programmatically then we need annotations. So this is the reason to go for annotation over comments.
There is another question may arise: if our requirement is to access meta data programmatically then we can use also xml document which can do, then why we need annotations. Then we will see what are the drawbacks of xml file and annotation eliminate that drawbacks.
To overcome these problems we go annotation s provided by jdk5.0 versions. In all J2EE technologies we use annotations as the replacement of xml documents.
So here gave the clarification why annotations came to the picture over comments and over xml documents.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc