为什么 Java 数组声明使用大括号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5293670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do Java array declarations use curly brackets?
提问by Evan
I would like to know why Java array declarations use curly brackets as opposed to the standard parenthesis. As illustrated here. I imagine this may take further understanding of curly brackets in general, but this specifically is on my agenda right now.
我想知道为什么 Java 数组声明使用大括号而不是标准括号。如此处所示。我想这可能需要进一步了解大括号,但这具体是我现在的议程。
Object[] tableHeaders = {"Cars","Trucks","Tacos"};
This is correct, as opposed to.
这是正确的,而不是。
Object[] tableHeaders = ("Cars","Trucks","Tacos");
回答by Eric
Curly brackets usually denotes sets and ensembles while parenthesis usually denotes parameters in C-like languages.
大括号通常表示集合和集合,而括号通常表示类 C 语言中的参数。
A long time ago, people got used to having this kind of convention with C. I'm pretty sure that it works this way in Java to keep some kind of syntax consistency with older languages.
回答by Stephen C
I can't say whyJames Gosling et al chose this particular alternative ...
我不能说为什么James Gosling 等人选择了这个特殊的替代方案......
However, using round brackets instead of curly brackets would create a serious ambiguity problem for the Java grammar. Consider the following example:
但是,使用圆括号而不是大括号会给 Java 语法带来严重的歧义问题。考虑以下示例:
Object[] tableHeaders = ("Cars");
Do the round brackets denote an array constructor, or are they part of the normal expression expression syntax?
圆括号表示数组构造函数,还是正常表达式表达式语法的一部分?
Now it would be possible to come up with some complicated precedence rules to cover this, but that makes life difficult for both programmers and compiler writers.
现在有可能想出一些复杂的优先规则来解决这个问题,但这对程序员和编译器编写者来说都很难。
(At the grammatical level, a Java parser needs to deal with ambiguity in:
(在语法层面,Java 解析器需要处理以下方面的歧义:
VariableInitializer:
ArrayInitializer
Expression
In the example above, ("Cars")
matches both the ArrayInitializer and Expression productions ... if you use round brackets for array initializers. Deciding which is correct meaning would require looking at the declared variable type ... followed by context sensitive parsing of the VariableInitializer. Nasty.)
在上面的示例中,("Cars")
匹配 ArrayInitializer 和 Expression 产生式……如果您对数组初始值设定项使用圆括号。决定哪个是正确的含义需要查看声明的变量类型......然后是 VariableInitializer 的上下文敏感解析。讨厌。)
I'd say they made the right choice in not using round brackets for array initializers.
我会说他们做出了正确的选择,即不将圆括号用于数组初始值设定项。
Square brackets won't work either. Consider trying to explain this ...
方括号也不起作用。考虑尝试解释这个......
... = new Object[]["Cars"]; // 1-D array
versus
相对
... = new Object[21][]; // 2-D array
versus
相对
... = new Object[][]; // is that 1-D or 2-D?
回答by slartibartfast
In most Algol-based programming languages, which are the most widely-used languages, arrays are declared using the curly braces.
在大多数基于 Algol 的编程语言(使用最广泛的语言)中,数组是使用花括号声明的。