8

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?

2
  • 1
    Have you seen a compilation error when using Maven? If so, what compiler were you using? Commented Oct 3, 2010 at 14:58
  • You can initialize lists in java in many ways as mentioned here stackoverflow.com/a/71661809/7397820 Commented May 27, 2022 at 15:41

3 Answers 3

15

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

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

Comments

4

From the Java Language Specification, section 10.6:

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

Comments

2

Sun javac has a bug wrt parsing trailing commas in annotations.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=284088

1 Comment

This is the actual JDK bug report: bugs.java.com/view_bug.do?bug_id=6337964 (fixed during Java 7's life, and backported to 6)

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.