今天看了一些Java代码,发现语法有点怪:
public class Sample {
public int get()[] {
return new int[]{1, 2, 3};
}
}
我认为无法编译并想修复我认为是错字的地方,但后来我记得 Java 编译器确实接受了它!
有人可以帮我理解它的意思吗?是函数数组吗?
最佳答案
这是一个返回 int[]
的方法。
Java Language Specification (8.4 Method Declarations)
For compatibility with older versions of the Java platform, a declaration form for a method that returns an array is allowed to place (some or all of) the empty bracket pairs that form the declaration of the array type after the parameter list.
This is supported by the obsolescent production:
MethodDeclarator:
MethodDeclarator [ ]but should not be used in new code.
关于java - Java方法签名后奇怪的 "[]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4699309/