I came across this tutorial and was wondering why the Java compiler would throw an error for the last example. Could someone explain?
Here's the excerpt:
--
Java Ugliness: Syntactic Irregularity and Ad Hoc Logic
In this irregular but convenient syntax: int[] v = {3,4};
, it does several things in one shot: {array type declaration, value assignment, number of elements declaration, slots fulfillment}. However, this syntactical idiosyncrasy cannot be used generally. For example, the following is a syntax error:
int[] v = new int[2];
v = {3, 4};
Here's a complete code you can try.
public class H {
public static void main(String[] args) {
int[] v = new int[2];
v = {3,4};
System.out.print(v[0]);
}
}
The compiler error is: “illegal start of expression
”.