gpt4 book ai didi

带括号语法的 Java 函数定义

转载 作者:IT老高 更新时间:2023-10-28 20:28:37 25 4
gpt4 key购买 nike

正在寻找 ByteArrayOutputStream 的源代码,我看到了这个函数:

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

此语法记录在哪里?我的意思是函数前面的 [] 。这是否与声明一个常规数组相同,其中括号可以放在数组名称之后或之前,但在这种情况下,括号可以放在函数名称之后?

String[] args;

对比

String args[];

编辑:2018-05-22

我在这里发现了这种疯狂语法的更多用途:10 things you didn't know about Java

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

最佳答案

JLS Sec 8.4 :

MethodDeclarator:
Identifier ( [FormalParameterList] ) [Dims]

...

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 函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50162893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com