tags:

views:

56

answers:

2

The comma , items separator used in an array initialization list may end the list in C, this is mentioned in The C Programming Language 2nd ed by Kernighan & Ritchie .

e.g.

  int c[] = { 1, 2, 3, };

This is convenient when the list is long, and one doesn't want to have to change/check the previous line when adding items

  long long c[] = { 
                    22342342344,
                     4324234234,
                    12312311111,
                   };

However in Java I could observe two different behaviors:
In Eclipse, the ending , is accepted while some versions of the maven compiler plugin complain and throw a compilation error.

However, I didn't find any mention of this singularity in the Flanagan's Java book.

What is the official rule regarding an ending comma after the initialization items?
Is it recommended not to use it?

+7  A: 

Section 10.6 of the spec explicitly says that a trailing comma is allowed (and ignored):

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

Link

Pointy
+4  A: 

From the Java Language Specification, section 10.6:

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

Richard Fearn