Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Java array declarations use curly brackets?

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.

Object[] tableHeaders = {"Cars","Trucks","Tacos"};

This is correct, as opposed to.

Object[] tableHeaders = ("Cars","Trucks","Tacos");
like image 366
Evan Avatar asked Mar 14 '11 00:03

Evan


People also ask

Why do we use curly brackets in Java?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What is the meaning of {} in Java?

In Java when you open a curly brace it means that you open a new scope (usually it's a nested scope). One important thing to understand is that a variable that you'll define inside this scope (which end where you put the closing curly brace) will not be recognized outside of the scope.

What are {} used for?

Brackets are used to insert explanations, corrections, clarifications, or comments into quoted material. Brackets are always used in pairs; you must have both an opening and a closing bracket.

How are curly braces used for an array?

{} (curly braces) Define the beginning and end of functions blocks and statement blocks such as the for and if structures. Curly braces are also used for defining initial values in array declarations.


2 Answers

Curly brackets usually denotes sets and ensembles while parenthesis usually denotes parameters in C-like languages.

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.

like image 104
Eric Avatar answered Nov 08 '22 14:11

Eric


I can't say why James Gosling et al chose this particular alternative ...

However, using round brackets instead of curly brackets would create a serious ambiguity problem for the Java grammar. Consider the following example:

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:

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.)

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?
like image 27
Stephen C Avatar answered Nov 08 '22 13:11

Stephen C