Summarize the role and history of Java annotations.
The "@" annotation is introduced in Java 1.5.
--It was born as a comment that gives a hint to the compiler, such as @Override
.
--Code accuracy: Annotations such as @ Nullable
, @ Deprecated
, and @Override
can add important semantic information to methods and fields so that the compiler enforces that information. it can.
--Test Method: Until TestNG and annotations appeared, JUnit used reflection to determine that a method was a test method. This determination required a special naming convention.
The adoption of annotations eliminates the need to use naming conventions.
--Persistence (Hibernate, etc.): By adding annotations, fields and methods can be associated with data stored in the database.
--Dependency injection: You can annotate a class that needs to be injected to indicate that.
@Nullable
and @ Nonnull
(javax.annotation)--Can it be attached to field and method parameters and the variable can take a null value? Please specify. --This annotation is very useful and many tools that use the Java compiler (javac) (the major IDEs are typical examples) recognize these two. --Whenever possible, always use this annotation. You'll soon notice that the number of null pointer exceptions in your code base drops sharply.
@Override
Mandatory since Java6.
--This annotation must be added to all methods that override the methods of the parent interface or parent class. --This addition causes the method to be overridden unintentionally.
--On the contrary, you can prevent the situation where you think that you have "overridden" a method that was not overridden due to a typo.
https://www.oracle.com/webfolder/technetwork/jp/javamagazine/Java-MA16-Annotations.pdf
Recommended Posts