[java annotation]

For anyone learning Java, annotations are a part that cannot be ignored. Annotations are a powerful tool in Java that allow programmers to add metadata to source code, thereby affecting the behavior of the compiler or JVM. This article will give you an in-depth understanding of what Java annotations are, what they can do, and how to use them in your code

Insert image description here

1. Definition of annotation

What are annotations? Annotations are special comments used to provide additional information to the code. It can be used to describe the properties, functions or behaviors of elements such as classes, methods, fields, etc. Annotations do not directly affect the running of the program, but can be read and processed by the compiler, development tools, or runtime environment.

Let’s use a real-life example to better understand how annotations work. Suppose you work in a bookstore and your task is to organize the books on the shelves. There are no labels on the books to tell you where to place them, so you need to examine the contents of each book individually to decide its placement. However, if the book has tags on it (e.g., "Science Fiction," "History"), then you can quickly put the book on the correct shelf based on that information without having to read the contents of each book.

Likewise, Java annotations are like these tags, they provide some additional information about the code. For example, if you have a method that you know may throw an exception, you can add an annotation to the method to tell the compiler this. This way, when someone tries to call this method, the compiler will automatically check if there is any code to handle the exception. If not, the compiler will issue an error message to let you know that you need to add exception handling code. This is a simple example of annotation

The role and advantages of annotations The main role of annotations is to add metadata to the code so that developers can better understand and maintain the code. It can be used in the following scenarios:

2. Custom annotations

Syntax and usage of annotations The definition of an annotation starts with the "@" symbol, followed by the name and parameters of the annotation. Annotations can be applied anywhere in the code, including classes, methods, fields, etc. When in use, the annotation information can be obtained through the reflection mechanism and processed accordingly as needed.

// 1. 定义一个接口,继承自java.lang.annotation.Annotation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD) // 指定注解可以应用在方法上
@Retention(RetentionPolicy.RUNTIME) // 指定注解在运行时可用
public @interface MyCustomAnnotation {
    
    
    String value() default ""; // 定义一个属性,类型为String,默认值为空字符串
}

// 2. 在需要使用自定义注解的地方,使用@注解名的形式来引用注解
public class TestClass {
    
    
    @MyCustomAnnotation(value = "Hello, World!")
    public void myMethod() {
    
    
        System.out.println("This is a test method.");
    }
}


Common annotation types There are many built-in annotation types in Java, such as @Override, @Deprecated, @SuppressWarnings, etc. In addition, there are many custom annotations provided by third-party libraries for functional expansion in specific areas.

Guess you like

Origin blog.csdn.net/weixin_55939638/article/details/134592406