-2

what is the difference between these 2 array staments in java. In Statement 1 we dont use new keyword so my question is there object is created or not.

Statement 1: int[] arr = {10,20,30};

Statement 2: int[] arr = new int[]{10,20,30};

1 Answers1

1

Both are the same, just a different way of writing:

  1. In the first statement, the type is derived by the compiler in the array creation from the types / the declaration of arr.
  2. In the second statement you have an explicit declaration of the instance. It contains more overhead.
s.fuhrm
  • 438
  • 4
  • 9