Java uses @interface annotation{} to define an annotation @Annotation, and one annotation is a class.
@Override, @Deprecated, @SuppressWarnings 3 Common Annotations.
Annotations are equivalent to a mark, and adding annotations to a program is tantamount to adding some kind of markup to the program,
Javac compilers, Development tools and other programs can use reflection to understand your class and the various elements have no markup, see what you have the mark, go to do the corresponding thing.
Note @override is used on methods, when we want to rewrite a method, add @override to the method, when we approach
When the name is wrong, the compiler will error
Annotation @deprecated, which is used to indicate that a Class's properties or methods are obsolete and do not want others to use in properties and methods
Modified with @deprecated,
Note @suppresswarnings is used to suppress warnings from the program, such as when no generics are used or the method is Obsolete.
Annotation @retention can be used to modify annotations, which are annotations, called Meta-annotations.
The retention annotation has an attribute value, which is of type retentionpolicy, and enum Retentionpolicy is an enumeration type,
This enumeration determines how retention annotations should be maintained, and can also be understood as rentention with Rententionpolicy. Retentionpolicy has 3 values:CLASS RUNTIME SOURCE
Annotated with @retention (retentionpolicy.class), which indicates that the information of the annotations is kept in the CLASS file (bytecode File) when the program is compiled, but is not read by the virtual machine at run time;
@retention (retentionpolicy.source) modified annotations, indicating that the information of the annotations will be discarded by the compiler, will not be left in the class file, the information will only remain in the source file;
Annotated with @retention (retentionpolicy.runtime), indicating that the information of the annotations is retained in the class file (bytecode File) when the program compiles, the virtual machine is kept at runtime,
So they can be read in a reflective way. Retentionpolicy.runtime allows you to read the annotation annotation information from the JVM so that it can be used when analyzing the Program.
packagecom.self;Importjava.lang.annotation.Retention;Importjava.lang.annotation.RetentionPolicy, @Retention (retentionpolicy.runtime) public@Interfacemytarget{} defines an annotated @mytarget, modified with retentionpolicy.runtime; packagecom.self;Importjava.lang.reflect.Method; public classMytargettest{@MyTarget public voiddosomething () {System.out.println ("hello world"); } public Static voidMain (string[] Args)throwsException {method Method= Mytargettest.class. GetMethod ("dosomething",NULL); if(method.isannotationpresent (mytarget.class))//true if annotation @mytarget is present on the DoSomething method{System.out.println (method.getannotation (mytarget.class)); }}} above program Print: @com. self.mytarget (), If the Retentionpolicy value is not runtime, it is not printed. @Retention (retentionpolicy.source) public@Interfaceoverride@retention (retentionpolicy.source) public@Interfacesuppresswarnings@retention (retentionpolicy.runtime) public@Interfaceas can be seen from the deprecated, only annotations @deprecated can be read into annotations by the JVM at run time to define attributes, see Example: @Retention (retentionpolicy.runtime) public@Interfacemyannotation{String Hello ()default"gege"; String World (); int[] Array ()default{2, 4, 5, 6 }; Enumtest.trafficlamp Lamp (); Testannotation lannotation ()default@TestAnnotation (value = "ddd"); Class style ()defaultString.class;} In the above program, define an annotation @myannotation, define 6 attributes, their name is: Hello,world,array,lamp,lannotation,style. property Hello type is string, The default value is Gege property, World type is string, there is no default Value property array type is an array, default value is 2,4,5,6attribute lamp Type is an enumeration, there is no default Value property lannotation type is annotation, default value is @testannotation, annotation property is annotation property style type is class, The default value is String type class type See the following example: defines a mytest class, with annotations @myannotation adornments, annotation @myannotation defined properties are assigned a value @myannotation (hello= "beijing", world= "shanghai", array={},lamp=trafficlamp.red,style=int.class) public classmytest{@MyAnnotation (lannotation[email protected] (value= "baby"), world = "shanghai", array={1,2,3},lamp=trafficlamp.yellow) @Deprecated @SuppressWarnings ("") public voidoutput () {System.out.println ("output something!"); The information for the annotations is then read by Reflection: public classmyreflection{ public Static voidMain (string[] Args)throwsException {MyTest MyTest=NewMyTest (); Class<MyTest> C = MyTest.class; Method Method= C.getmethod ("output",Newclass[] {}); //true if the MyTest class name has annotations @myannotation adornments if(MyTest.class. isannotationpresent (myannotation.class) {System.out.println ("have annotation"); } if(method.isannotationpresent (myannotation.class) {method.invoke (myTest,NULL);//call the Output method//get information about annotation @myannotation on a methodMyannotation myannotation = method.getannotation (myannotation.class); String Hello=Myannotation.hello (); String world=Myannotation.world (); System.out.println (hello+ "," + world);//Print Properties Hello and World valuesSystem.out.println (myannotation.array (). length);//Print Properties The length of array arraysSystem.out.println (myannotation.lannotation (). Value ());//Print the value of the property lannotationSystem.out.println (myannotation.style ()); } //get all the annotations on the output method, of course, are retentionpolicy.runtime modifiedannotation[] annotations =method.getannotations (); for(Annotation annotation:annotations) {System.out.println (annotation.annotationtype (). getName ()); }}} above program print: have annotationoutput something!gege, Shanghai3Babyclassjava.lang.Stringcom.heima.annotation.MyAnnotationjava.lang.Deprecated If there is an attribute name called value in the annotation, you can omit the property name when you apply it. visible, in the @Retention (retentionpolicy.runtime) annotation, retentionpolicy.runtime is the annotation attribute value, the property name is value, and the return type of the property is retentionpolicy, as Follows: public@Interfacemytarget{String Value ();} Can be used like this: @MyTarget ("aaa") public voiddosomething () {System.out.println ("hello world"); The annotation @target is also a meta-annotation used to modify annotations, which has an attribute ElementType is also an enumeration type with the Value: annotation_type CONSTRUCTOR FIELD local_variable METHOD Package A PARAMETER type such as @target (ELEMENTTYPE.METHOD) modifies the annotation to indicate that the annotation can only be used to decorate the method. @Target (ELEMENTTYPE.METHOD) @Retention (retentionpolicy.runtime) public@Interfacemytarget{String value ()default"hahaha";} If the @mytarget decoration on the class, then the program error, such as: @MyTarget public classmytargettest annotations are mostly used in the development framework, well, the comments on the study so much, thank You.
Java Comment @interface the use of "go"