Two Laptop Bag
The moose likes Beginning Java and the fly likes int[] x = {1, } Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int[] x = {1, }" Watch "int[] x = {1, }" New topic

int[] x = {1, }

Peter Klotz

Joined: Jul 30, 2008
Posts: 6
In one of the K&B examples in the SCJP5-book if found a line of code that I do not quit understand.

Does it make sense to have a comma followed by nothing in the array-initialization


Code compiles and runs fine.
Is that a bug or a feature?
Thanks for an answer,
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
A feature. See section 10 of the Java Language Specification
A trailing comma may appear after the last expression in an array initializer and is ignored.

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

This was included (to my understanding) so that in this case the Java syntax would match the C syntax. I think I once knew why this was the case in C, but I now forget.
Peter Klotz

Joined: Jul 30, 2008
Posts: 6
Thank you Joanne and David!
Hendy Setyo Mulyo
Ranch Hand

Joined: Dec 01, 2004
Posts: 219

Is that feature also applicable for Java 1.3 and Java 1.4?

Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: int[] x = {1, }
It's not a secret anymore!