1

Could anyone please explain what is the necessity of annotations in java? OR Let me put it more specifically as 'What would have happened if there were annotations in java?'. I have often seen @Override annotation used extensively. But code can compile even without this annotation then what is it's need? I do understand this makes code less prone to spell mistakes but are annotations optional in general?

1 Answers1

1

To quote the Java Tutorials:

Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

That does not mean that they are not very useful. The value of the @Override is demonstrated when you apply the annotation to a method that does not actually override any superclass method. The Java compiler will generate an error that alerts the developer that they are not overriding the method they think they are. This effectively turns a run-time error into a compile-time error.

Annotations are also widely used in a number of Java libraries and external libraries. For example, JAXB utilizes annotations to customize the XML text output from its bindings, and Hiberrnate uses annotations to map Java classes to relational database tables.

DMunz
  • 253
  • 2
  • 7