aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int[] x = {1, }" Watch "int[] x = {1, }" New topic
Author

int[] x = {1, }

Peter Klotz
Greenhorn

Joined: Jul 30, 2008
Posts: 6
Hello,
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,
Peter
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
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.


Joanne
David O'Meara
Rancher

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
Greenhorn

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%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int[] x = {1, }