• Post Reply Bookmark Topic Watch Topic
  • New Topic

Very strange array initialization  RSS feed

 
Alexandru Gifei
Ranch Hand
Posts: 42
Java Linux Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Anyone knows how is this possible/what's the explanation? I happened to find this instruction in a project i am working on and also here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html.

I noticed that this statement also compiles:

If i am trying to print the 4th element in the array i am getting an IndexOutOfBounds Exception.

Thank you,
Looking forward for an answer.
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alexander,



This code won't compile.

Please check once again.

However,

Following code will compile fine.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote: . . .

Following code will compile fine. . . .]
But it produces a 5‑element String[] as opposed to a 3‑element array. I know you are allowed one comma dangling at the end of an array initialiser; obviously you are not allowed two commas. Yes, look at this Java Language Specification page; it permits one optional comma.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell

Thanks for posting this information about dangling comma.
I did not knew it before.

That's what I like about JAVA ... always have some thing new in store...

Thanks
Abhay Agarwal

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal wrote: . . .
I did not knew it before.
. . .
I didn't know it properly either. And … you're welcome
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal wrote:Thanks for posting this information about dangling comma.

Was that what you were referring to, or were you surprised that you could initialise an array simply with braces?

As far as a I know, that feature is restricted only to String and int arrays (although it may include other primitive/wrapper types), and is simply a form of "shorthand". Personally, I still use:
String[] as = new String[] { "array", "of", ... };

Winston
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dangling comma is a useful feature, I've used it in my code. Looks sort of like this:



That way all of the lines are consistent and you don't have to make sure you get the last comma (or lack thereof) right when you add another entry or remove one.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The dangling comma is a useful feature, I've used it in my code.

Oddly enough, I never have. :wink:

So does your example produce a 4 or 5 element array?

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:So does your example produce a 4 or 5 element array?

Why not try it for yourself and see what happens

(On my system it produces a 4 element array.)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Why not try it for yourself and see what happens

Ach, I was feeling lazy. But thanks for the info.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!