Java Annotations

Java Annotations
  1. Annotations are introduced in java 5.0.
  2. Annotations allow us to provide metadata (data about data) information into our source code.
  3. In java all annotations are interfaces only. 
  4. Already in Java to provide metadata already we have comments right? then what is the need of Java Annotations?

In any programming language, compilation is work from following 4 machines

  1. Pre-Processor
  2. Compiler
  3. Assembler
  4. Loader / Link editor

In Java compilation phase pre-processor is not existed. Because in java #include statements are not available. Compiler phase existed 6 number of phases.

1. Lexical Analyzer

Lexical analyzer phase is the first phase of compilation process. It takes source code as input. It reads the program and converts it into tokens. It converts a stream of lexemes into a stream of tokens. Tokens are defined by regular expressions which are understood by the lexical analyzer. It also removes white-spaces and comments.

2. Syntax Analysis

Syntax analysis is the second phase of compilation process. It takes tokens as input and generates a parse tree as output. In syntax analysis phase, the parser checks that the expression made by the tokens is syntactically correct or not.

3. Semantic Analysis

Semantic analysis is the third phase of compilation process. It checks whether the parse tree follows the rules of language. Semantic analyzer keeps track of identifiers, their types and expressions. The output of semantic analysis phase is the annotated tree syntax.

4. Intermediate Code Generation

In the intermediate code generation, compiler generates the source code into the intermediate code. Intermediate code is generated between the high-level language and the machine language. The intermediate code should be generated in such a way that you can easily translate it into the target machine code.

5. Code Optimization

Code optimization is an optional phase. It is used to improve the intermediate code so that the output of the program could run faster and take less space. Optimization can be assumed as something that removes unnecessary code lines and arranges the sequence of statements in order to speed up the program execution without wasting resources (CPU, memory). 

6. Code Generation

Code generation is the final stage of the compilation process. It takes the optimized intermediate code as input and maps it to the target machine language. Code generator translates the intermediate code into the machine code of the specified computer.

So, in lexical analysis phase the specified complete meta data in the form of comments would be removed from our source program. 

If you open the .class file then meta data is not available. But my requirement is I want bring my meta data to up to .java file, up to .class file and up to run time (to debug or test our application). So, if you want test or debug your application then comments are not sufficient. So, we must go for annotations.

Java Annotations

Scroll to top