java - Java方法签名后奇怪的 "[]"

标签 java arrays syntax

今天看了一些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/

相关文章:

java - 在 JFileChooser 上使用 JComboBox 列出以组合框第一行文本开头的所有文件

java - 将输入拆分为 vector

javascript - 在 JavaScript 中循环遍历数组

java - 如何作为类成员从数组中获取长度?

Javascript (jQuery) 错误 : SyntaxError: missing ) after argument list

java - 在已部署的 java war 文件之外配置文件是否安全?

java - 使用 Google Media Framework Exoplayer 的视频播放器空指针异常

javascript - 根据 React 中的输入字段过滤对象数组

shell - expr 中的冒号运算符有什么作用?

javascript - 什么{| ... |} 在 javascript 中是什么意思?