0

The following line of code compiles. Java allows this shortcut.

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

But the following does not compile, why?

int[] ay;
ay = {1,2,3,4,5};
Rey Libutan
  • 5,226
  • 9
  • 42
  • 73
North
  • 21
  • 4
  • 1
    Because the second line is an assignment. Assignment is not the same as initialisation. But note that `int[] az = {1,2,3,4,5};` is just syntax sugar for `int[] az = new int[] {1,2,3,4,5};`. – Oliver Charlesworth Jul 14 '15 at 02:09
  • Why, I don't know, but you can't use the short cut form on a separate line, you need to use `ay = new int[]{1,2,3,4,5};` – MadProgrammer Jul 14 '15 at 02:10
  • It *is* allowed in a separate line. It isn't allowed in a separate *statement.* – user207421 Jul 14 '15 at 02:24

0 Answers0