• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wait a minute, Why is this syntax allowed?  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!... java enthusiasts. I have a question about arrays, hope you can provide me an explanation. I don't understand why the compiler acts so weird in this case:



The preceding code compiles with no errors, But when you run the program it throws an ArrayOutOfBoundsException. it's understandable because there's no element at that position,

But Why Does the compiler allow that syntax? --> ({1,2,})

A comma and then no element... the compiler should freak out.

Well, That would be my query.
Have a nice day,

Jose
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell you why its ok, but here it is from the JLS:

10.6 Array InitializersAn array initializer may be specified in a declaration, or as part of an array creation expression (�15.10), creating an array and providing some initial values:

ArrayInitializer:
{ VariableInitializersopt ,opt }

VariableInitializers:
VariableInitializer
VariableInitializers , VariableInitializer

The following is repeated from �8.3 to make the presentation here clearer:

VariableInitializer:
Expression
ArrayInitializer

An array initializer is written as a comma-separated list of expressions, enclosed by braces "{" and "}".
The length of the constructed array will equal the number of expressions.

The expressions in an array initializer are executed from left to right in the textual order they occur in the source code. The nth variable initializer specifies the value of the n-1st array component. Each expression must be assignment-compatible (�5.2) with the array's component type, or a compile-time error results.

If the component type is itself an array type, then the expression specifying a component may itself be an array initializer; that is, array initializers may be nested.

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

As an example:

class Test {
public static void main(String[] args) {
int ia[][] = { {1, 2}, null };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
System.out.println(ia[i][j]);
}
}

prints:

1
2

before causing a NullPointerException in trying to index the second component of the array ia, which is a null reference.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you adam.

Is that from the java language specification? hmmmmmm, interesting... definitely interesting.

If anyone has any further comments please let me know.

Thanks for your time,
Sincerely,
Jose
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's from the JLS. It's allowed mainly because it's convenient.

String[] words = {
"door",
"goodbye",
"hello",
"house",
};

Every line is the same, which makes sorting the lines, inserting new lines, and deleting old ones all more convenient and less error prone. It's also easier to generate code like this automatically, because every data line has a comma on it, rather than making the last one special.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow...
I never thought about it that way, even when sometimes I had to initialize some arrays in that precise way. It seems the java engineers have thought about everything.

Thanks Ernst, your answer has satisfied my doubt completely.
Keep on with the awesome work you do here !

Thank you,
Sincerely,
Jose
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note, though, that there currently is a bug in Sun's compiler that prevents this notation from being used in Annotations.

That is, the following, as far as I remember, will correctly compile using Eclipse, but not using javac:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:
Wow...
I never thought about it that way, even when sometimes I had to initialize some arrays in that precise way. It seems the java engineers have thought about everything.

Well, I don't think the Java engineers were the first ones who invented this. C and C++ also have this feature, and Java borrows a lot from C and C++.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies everyone!
Oh yeah, that C++ is quite something, right? I was never quite clear on which one is better java or C++.........

Until next time,
Thanks again,
Jose
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!