Use of Java Notes in Java Notes

tags: java

annotation

①Annotation is a special "comment" placed before the classes, methods, fields, and parameters of the Java source code
② Comments will be directly ignored by the compiler, and comments can be packaged into the class file by the compiler. Therefore, comments are a kind of "metadata" used as annotations (to be more popular, It is the description or function added for this method.)

    @Override//For example: @Overvide this annotation is used to explain this way to rewrite the parent class
    public String toString() {
        return "Hello";
    }

The role of annotations

①The annotation itself has no influence on the code logic, how to use the annotation is completely determined by the tool
② The first type is the annotation used by the compiler, for example: @Override (let the compiler check whether the method is overridden correctly) This type of annotation will not be compiled into. class files, they are thrown away by the compiler after compilation.
③ The second category is the annotations used by tools to process .class files. For example, some tools will dynamically modify the class when loading the class to achieve some special functions. Such annotations will be compiled into the .class file, but will not exist in the memory after loading. This type of annotation is only used by some underlying libraries, and generally we don't have to deal with it ourselves.
④ The third type is the annotations that can be read during the running of the program. They always exist in the JVM after being loaded. This is also the most commonly used annotation. For example, a method configured with @PostConstruct will be automatically called after the constructor is called (this is the function implemented by the Java code reading the annotation, and the JVM will not recognize the annotation).
⑤ When defining an annotation, you can also define configuration parameters.
⑥ The configuration parameters can include: all basic types; String; enumerated types; arrays.
⑦Because the configuration parameters must be constants, the above restrictions ensure that the annotations have determined the value of each parameter when they are defined.
⑧ Annotated configuration parameters can have default values, and default values ​​will be used when a certain configuration parameter is missing.
⑨Most comments will have a configuration parameter named value. To assign a value to this parameter, you can just write a constant, which is equivalent to omitting the value parameter.

//@Check is a comment
public class Hello {
    @Check(min=0, max=100, value=55)//Three parameters are clearly defined
    public int n;
     @Check(value=99)//Only value is defined
    public int p;
    @Check(99) // @Check(value=99) omits the value parameter, only he can write this
    public int x;
    @Check//All parameters are default values
    public int y;
}

Definition annotation

①Java language uses @interface syntax to define annotations (Annotation)
② Each method in the annotation represents a configuration parameter, the return type is the parameter type, and the default value of the parameter can be identified by default.
③The most commonly used parameter should be named value

public @interface Report {
    int type() default 0;
    String value() default "";
}

Meta annotation

① There are some annotations that can modify other annotations, and these annotations are called meta annotations.
②The Java standard library has defined some meta-annotations, we only need to use meta-annotations, and usually do not need to write meta-annotations by ourselves.
The following are four meta-annotations provided by Java

How to customize Annotation

The first step is to define annotations with @interface:

public @interface Report {
}

The second step is to add parameters and default values:

public @interface Report {
    int type() default 0;
    String level() default "info";
    String value() default "";
}
 Define the most commonly used parameters asvalue(), It is recommended to set default values ​​for all parameters.

The third step is to configure annotations with meta annotations:

@Target(ElementType.TYPE)//You must set @Target to specify the scope to which Annotation can be applied
@Retention(RetentionPolicy.RUNTIME)//Easy to read the Annotation during runtime
public @interface Report {
    int type() default 0;
    String level() default "info";
    String value() default "";
}

Among them, @Target and @Retention must be set, and @Retention is generally set to RUNTIME, because our custom annotations usually require reading during runtime. Under normal circumstances, it is not necessary to write @Inherited and @Repeatable.

Intelligent Recommendation

Joiner's use notes in Java

The required jar package address 1. Separate a list and output a string Output results 2. Filter the NULL value inside Output results 3. Replace the NULL value inside 4. Use stream to perform string s...

[Java Notes] - the use of printf in Java

Detailed explanation of usage in printf in java Printf supports the following formats The full format of printf's format control: % - 0 m.n l or h format characters The following describes the items t...

Notes Notes Notes JAVA

And complaints made about irrelevant topics:        Print: sss A, java built-notes     - J0 y. E* ?9 c1 C6 R' t5 Z& l     0 E( x( H...

Java notes - final use (new notes)

Use of final in Java Final keyword Using the final keyword to do the logo has the "final" meaning Final can modify classes, methods, properties, and variables final modification class, this ...

Java Notes-Notes on the use of CommandLineRunner in SpringBoot

It can be seen that this is an interface in SpringBoot. After the Spring environment is set up, the calls are made one by one in the main thread, as follows: The example code is as follows: DisposeOne...

More Recommendation

Java notes: the characteristics and use of interfaces

interface 1.Interface definition * The modifier can be public or omitted. If public is omitted, the package permission access control is used by default (the interface can be accessed in the same pack...

Java notes - definition and use of generics

1. Generic definition Receives a data type via <data type>, which is used at compile time to detect elements in the collection. If it is not the type specified in <>, it is not allowed to ...

Use of java study notes --xml

Use the xml   First posted Code: books.xml: <?xml version="1.0" encoding="UTF-8"?> <books>     <book sn="SN12341232"> <name> e...

JAVA notes - thread pool use

First, the thread pool Second, use the thread pool Third, the thread pool code examples Fourth, the principle of the thread pool corePoolSize: core thread pool, maximumPoolSize: maximum number of thre...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top