Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int[] x = {1, }

 
Peter Klotz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joanne and David!
 
Hendy Setyo Mulyo
Ranch Hand
Posts: 219
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that feature also applicable for Java 1.3 and Java 1.4?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic