0

Java

When creating an array using the new keyword, why are parentheses not used, given that an array is an object?

int [] arr= new int[5];
rahs
  • 1,759
  • 2
  • 15
  • 31
  • 3
    Because the designers of Java chose to use a syntax that would be familiar to C(++) developers. That's also why you can write `int[] arr` **or** `int arr[]`. – Elliott Frisch Jul 08 '18 at 06:22
  • @ElliottFrisch Are arrays the only exceptions to new objects being created (using the new keyword) using parentheses? – rahs Jul 08 '18 at 06:40
  • 1
    No. There's also autoboxing. `Integer i = 1;` **and** `String`(s). `String a = "Hello";` – Elliott Frisch Jul 08 '18 at 06:42
  • It is also because they would be redundant. The compiler already knows it is an array-creation expression, and that there will never be any arguments, so there is no point in requiring them. – user207421 Jul 08 '18 at 06:43

0 Answers0