[Java] Java annotation history

1 minute read

Summarize the role and history of Java annotations.

history

The “@” annotation was introduced in Java 1.5,

  • It was born as a comment that gives a hint to the compiler like @Override.

Benefits of using annotations.

  • Code correctness: annotations such as @Nullable, @Deprecated, and @Override can add important semantic information to methods and fields and force the compiler to enforce that information. it can.

  • Test method: Until the advent of TestNG and annotations, JUnit used reflection to determine that a method was a test method. This determination required special naming conventions.
    Adopting annotations eliminates the need to use naming conventions.

  • Persistence (Hibernate etc.): By adding annotations, fields and methods can be associated with the data stored in the database.

  • Dependency injection: Annotation can be added to the class that needs to be injected to indicate that.

Frequently used annotations

###@Nullable and @Nonnull (javax.annotation)

  • Can be attached to fields and method parameters, and variables can have null values Please specify.
  • This annotation is very useful and many tools that use the Java compiler (javac) (main IDEs are a good example) recognize the two.
  • Try to use this annotation whenever possible. You should immediately notice a dramatic decrease in the number of null pointer exceptions in your codebase.

###@Override

Required since Java 6.

Reason

  • This annotation must be added to all methods that override the methods of the parent interface or class.
  • Unintentionally overriding a method with this addition

  • On the contrary, it is possible to prevent a situation where you think that you have “overridden” a method that you have not overridden due to a typo.

Referenced article (Thank you always)


https://www.oracle.com/webfolder/technetwork/jp/javamagazine/Java-MA16-Annotations.pdf

Tags:

Updated: