• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why doesn't this give an error?  RSS feed

 
Pete Letkeman
Ranch Foreman
Posts: 898
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised this code does not error.

I would have thought that the array declarations would have generated a compile time error, not they don't.
However if you put a comma before "1" or the word "one" then you do get a compile time error, which is what one should expect.

Edit: Grammar mistake in subject line
 
Matthew Vahedi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Which IDE or compiler are you using?
 
Pete Letkeman
Ranch Foreman
Posts: 898
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Java 8 on Windows 10 Pro 64 bit edition and compiling and running using the command prompt.
From the command prompt:

java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)


You can see my Windows info, generated by running Winver, in the attached
win10.jpg
[Thumbnail for win10.jpg]
Results of Winver
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.6

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

So, it isn't an implementation bug. And after researching slightly more, it seems there are some semi-convincing reasons. Here.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember a question similar to this coming up ages ago. Let's look at the Java® Language Specification (=JLS). You see that an array initialiser has this structure:-
ArrayInitializer: { [VariableInitializerList] [,] }
which means that a comma after the end of the list is permitted, and a few lines later it says,
A trailing comma may appear after the last expression in an array initializer and is ignored.
When the question came up previously, somebody said maybe it makes it easier to add a new element if the comma is already there. It may also make it easier for automated code generators to use writer.write(nextElement + ", "); There is no need to worry about removing the final comma.
 
Pete Letkeman
Ranch Foreman
Posts: 898
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Liutauras Vilda and Campbell Ritchie.

I'll try to remember to look at the JLS before posting questions like this in the future.
 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
JLS wrote:A trailing comma may appear after the last expression in an array initializer and is ignored.

So, it isn't an implementation bug. And after researching slightly more, it seems there are some semi-convincing reasons. Here.


This is a great feature! When you have a very large array initializer (like in the ListResourceBundle which stores text strings for my bird taxonomy and listing application) it's much easier if each of the entries can be followed by a comma, instead of forcing the last entry to be a special case. Specifically, it's easier to add more entries to the end of the list.
 
Pete Letkeman
Ranch Foreman
Posts: 898
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:This is a great feature!
...
it's much easier if each of the entries can be followed by a comma, instead of forcing the last entry to be a special case.

I agree 100% with you Paul.
I know that some languages such as VBScript/Classic ASP would be not work with the extra comma at the end like with this.
I also wanted to be sure that I wasn't missing something with this as it could very well be the certification test which I'm preparing for.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!