default in Java interface, public String value() default "default";

When learning Java annotations, you usually encounter a keyword default:

@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface TestDocAnnotation {
public String name() default "value";
}

This piece of code is not easy to understand. In fact, it is equivalent to

public @interface TestDocAnnotation(public String name="value”){
}

It defines a parameter name of String type, and the default value is "value". The brackets after the name are just the way to define parameters in annotations.

We can use this annotation like this:

TestDocAnnotation(name=“para”)
class Test{}

The value "para" is assigned to name above. If it is not assigned a value:

TestDocAnnotation(name)
class Test{}

name is the default value "value".

Guess you like

Origin blog.csdn.net/m0_50401435/article/details/131717278