26

Array initializers can be used to initialize arrays at compile-time. An initializer with trailing commas as shown below compiles fine.

int a[][] = {{1,2,} ,{3,4,} , {5,6,},}; //Trailing commas cause no compiler error

for(int i=0;i<a.length;i++)
{
    for(int j=0;j<2;j++)
    {
        System.out.print(a[i][j]+"\t");
    }
    System.out.println();
}

Output :

1        2        
3        4        
5        6     

Also legal with one dimension arrays as obvious with the above discussion.

int[] b = {1, 2, 3, 4, 5, 6,}; //A trailing comma causes no compiler error

for(int i=0;i<b.length;i++)
{
    System.out.print(b[i]+"\t");
}

Output :

1        2        3        4        5        6

Even the following is a legal syntax and compiles fine.

int c[][] = {{,} ,{,} , {,},}; 

The compiler should expect a constant value (or another initializer) after and before a comma ,. How is this compiled? Does the compiler simply ignore such commas or something else happens in such a scenario?

7
  • 2
    A trailing comma is ignored. It's allowed by the language since some find it convenient. Commented Jul 23, 2012 at 23:18
  • 3
    @Daniel Well it keeps diffs more succinct when adding things to arrays, allows easy reordering of array expressions and enables a simpler grammar for the language all in one go. Quite some advantages for no downsides I can see. Commented Jul 24, 2012 at 0:00
  • @Voo How does it make the grammar simpler? If the trailing comma were mandatory, that would make it simpler, but with an optional trailing comma, I don't see it. Nor can I see the greater succinctness of diffs. Reordering, yes. Commented Jul 24, 2012 at 0:14
  • 1
    @Daniel Right you are about the grammar, I forgot that they're still optional. About diffs: Assume that for larger arrays every item gets its own line (I see that relatively often and while it's not always sensible, there are lots of situations where it's useful - also leaves place for comments without resorting to /* */ inlining). Adding a new line then only changes a single line in the diff, without it you'd have to change two. Commented Jul 24, 2012 at 0:22
  • @Voo I see. But if you lay out your arrays Haskell style, with leading commas, it wouldn't change two lines - except if you add an entry at the first position, but that's much rarer than adding at the end. Commented Jul 24, 2012 at 0:28

1 Answer 1

38

The trailing comma is ignored. From the Java specification:

A trailing comma may appear after the last expression in an array initializer and is ignored.

Sign up to request clarification or add additional context in comments.

1 Comment

as a side note: the above statement is present even in JLS 1.0 titanium.cs.berkeley.edu/doc/java-langspec-1.0 (reaching back to Java 1.0), so we can safely say "it was always there".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.