Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Extra Comma is allowed when constructing an array?

 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why do above 2 statements compile without even a warning?

Personally, I don't like these weired statements. Anyone can give me any reasonable excuses to help me accept them? Thanks in advance.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you all have with that extra comma today ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can imagine one reason : when you want to allow the array to grow in further version of your code, without touching the previous version. For example :

You know this array of product may change in the future. You can add a new product, without having to take care of the last one. If the code looked like this :

you'd have to add an extra comma.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you could also have

, but personnally, I prefer to have trailing commas
 
Martin Vajsar
Sheriff
Pie
Posts: 3751
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find this feature very useful. Apart from adding to the array, you can easily reorder the elements without having to tidy it up when the last element suddenly stops being last (if you have every element on its own line).

The same is allowed for enums, eg.I generate some Java enums from a database using a script, and not having to bother about the last constant having a semicolon instead of comma is great. Unfortunately, I learned about this feature after I've tailored the scripts to use semicolon instead of comma after the last value....
 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. Very helpful!
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously whoever wrote that part of the first Java™ compiler thought it was a good idea. You can see from this little bit of C coding that the additional comma was permitted long before Java™ was developed.
 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Campbell. Glad to know Java has this classical part!
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic