带括号语法的Java函数定义 [英] Java function definition with brackets syntax

查看:71
本文介绍了带括号语法的Java函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在源代码中查找ByteArrayOutputStream,我看到了此功能:

Was looking in the source for ByteArrayOutputStream, and I saw this function:

public synchronized byte toByteArray()[] {
    return Arrays.copyOf(buf, count);
}

此语法在哪里记录?我的意思是该功能前面的[].

Where is this syntax documented? I mean the [] in front of the function. Is this the same as in declaring a regular array where the bracket can go after the name of the array or before, but in this case, the bracket can go after the function name?

String[] args;

VS

String args[];


2018-05-22

我在这里发现了这种疯狂语法的更多用法:关于Java的十件事

I found even more uses of this crazy syntax here: 10 things you didn't know about Java

#3是他们提到上述语法可被利用的所有方式的地方

#3 is where they make mention of all the ways the above syntax can be exploited

推荐答案

MethodDeclarator:
    Identifier ( [FormalParameterList] ) [Dims]

...

允许返回数组的方法的声明将表示数组类型的部分或全部括号对放在形式参数列表之后.支持此语法以与Java编程语言的早期版本兼容.强烈建议在新代码中不要使用此语法.

The declaration of a method that returns an array is allowed to place some or all of the bracket pairs that denote the array type after the formal parameter list. This syntax is supported for compatibility with early versions of the Java programming language. It is very strongly recommended that this syntax is not used in new code.

这篇关于带括号语法的Java函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆