About the problem that the parameters in the java method are of array type?

recently I have seen several codes whose method parameters contain array types, but when passing arguments, they are not written in array form, that is, they are not surrounded by curly braces, but can actually run. What is the reason for this? Thank you! The
code is as follows:

example 1:

@WebServlet(description = "a enter for wechat", urlPatterns = { "/aaa"},loadOnStartup=1) 

example 2:

@WebServlet(description = "a enter for wechat", urlPatterns =  "/aaa",loadOnStartup=1) 

Webservlet comment source code:

    /**
     * @return array of URL patterns to which this Filter applies
     */
    String[] urlPatterns() default {};

question 1:
after reading the source code, I think that the assignment of urlPatterns should be written according to example 1, but in fact, many people use the method of example 2, but shouldn"t the assignment of example 2 be of string type instead of string array? Doesn"t that make the parameter types mismatch? But in fact, I tested and found that both ways of writing can be used. Why is that?

question 2: what if urlPatterns has more than one url instead of one? Is it necessary to write {url1,url2} in curly braces as shown in example 1?
Thank you!

May.01,2021

this is the syntax sugar of the compiler. The compiler will automatically write the parameters in the form of an array. Find a decompiler tool to take a look at it, for example:
source code:

clipboard.png

:

clipboard.png

similar scenarios also have variable parameters, which are automatically encapsulated by the compiler into an array

Menu