62

Possible Duplicate:
Arrays constants can only be used in initializers error

I was studying arrays, and I came through this short-cut method of declaring and initializing an array in one line. For example,

int[] a = {1, 2, 3, 4, 5};

But when I tried to do following code, I got this compiler error saying "Array constants can only be used in initializer".

int[] a;
a = {1, 2, 3, 4};

Why so?

4
  • 24
    This is not a duplicate. The OP is asking why not how.
    – Jeremy
    May 9, 2012 at 17:04
  • 11
    Meh; not convinced it's a dupe--linked question says "how do I do it so it works", this question asks "why doesn't the other way actually work". The difference leads to completely different answers--this question already has how to make it work. May 9, 2012 at 17:06
  • These sorts of questions are interesting, but pretty much impossible to answer -- all we can do is speculate. It'd be really interesting to ask this of whoever wrote that part of the spec, though, as a teaching moment (why did you do this design and not that?).
    – yshavit
    May 9, 2012 at 17:08
  • i have already gone through it i knw the ways to declare and initilize array jst wanted to knw the reason why above is not allowed. is their any logical reason behind it? May 9, 2012 at 17:10

4 Answers 4

116

It's not allowed because the JLS says so. The syntax is only permitted in declarations and in array creation expressions.

The latter provide an alternative way of achieving the same result:

int[] a;
a = new int[]{1, 2, 3, 4};

As to the actual underlying reason for requiring the new T[], my guess is as follows. Consider the following array initializer:

{1, 2, 3, 4}

It can be used to initialize arrays of different types:

new int[]{1, 2, 3, 4};
new float[]{1, 2, 3, 4};
new double[]{1, 2, 3, 4};

If the new T[] bit wasn't required, I suspect that the bare {1, 2, 3, 4} could cause difficulties during semantic analysis. Here, I am thinking about cases like:

void f(float[] x) { ... }
void f(double[] x) { ... }
void g() {
  f({1, 2, 3, 4});
}

If this syntax were allowed, the language spec would have to deal with the complexity of choosing which function to call.

In a similar vein, it's not clear what should be the type of {null}. It can be Object[], Integer[], Serializable[] and so on.

And finally, the empty array {} would be the trickiest of all. Here, we can't even tell if it's an array of objects or an array of scalars.

Instead of dealing with all these complexities, it seems that the language designers chose to avoid them by requiring the new T[] syntax.

7
  • that i knw but my question is why it is not allowed in tht way? May 9, 2012 at 17:00
  • Meh--the compiler knows the type of arrn. I could buy it's a compiler simplification, but there's nothing that difficult about it. May 9, 2012 at 17:10
  • 2
    @DaveNewton: Not necessarily. Consider the possibility of the bare {1, 2, 3, 4} being passed into a function that's overloaded for float[] and double[]. What should happen?
    – NPE
    May 9, 2012 at 17:12
  • @aix Same thing that happens now when it's not an array, it'd call the float[] method. May 9, 2012 at 17:19
  • Does that really happen now? Wouldn't that mean that the compilr goes through the array, narrow casts it as much as it can and then chooses the constructor? I'm glad that's not allowed...
    – Miquel
    May 9, 2012 at 17:22
9

The short answer is because the language spec says so.

As for why? I suspect it's down to typing. In the first case, the parser/compiler knows it's within the context of initialising an array variable, and so the curly braces can be inferred to be an array initialiser.

In the latter case, it's not immediately clear from the line what the curly braces mean. Presumably the typer runs at a later phase of parsing, such that it wasn't feasible to simply infer the meaning.

This argument seems to have weight in that you can use a very similar syntax if you specifically (and technically redundantly) declare the type again:

int[] a;
// then later
a = new int[] { 1, 2, 3, 4 };
1
  • 1
    Java is famous for its "technically redundant" syntax; there's no "technically" about it. May 9, 2012 at 17:07
2

The only answer you can get is of philosophical nature. The decision not to allow the implicit array type is in line with the general design principle of Java to keep things simple and obvious. In the same vein you could ask why every downcast must be explicit, or every narrowing type-conversion. Java is a blue-collar language and obvious+explicit is its core value.

0

I java you can only initialize an array using the first method. You cannot assign an array. Understanding why might involve some theory on how arrays are implemented. The compiler has to know how large an array when the array is declared so with the declaration and initialization on the first line, the compiler can infer the size but not with the second.

3
  • 2
    OP already knows the second one doesn't work and is asking why. Answer doesn't address that at all. May 9, 2012 at 17:02
  • why complier can read no of element in curly brasce and know the size the way it does in 1st case - int[] a={1,2,3}; May 9, 2012 at 17:06
  • java is a statically typed so the compiler has to know the size of array at the point of initialization so it can allocate memory for the array.
    – cobie
    May 9, 2012 at 17:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.