1

I want to know what will happen to the array using this.

double [] array = new double[] {0}

does it initialize an array that all of its elements are zero? and if so what will the length of the array be.

  • 2
    Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Hülya Nov 27 '20 at 09:05
  • Note that since the default value for a `double` is already zero, this code is exactly equivalent to `new double[1]`. – chrylis -cautiouslyoptimistic- Nov 27 '20 at 09:35

2 Answers2

1

It initializes the array with the first and only element being 0

R4yY
  • 116
  • 12
0

The given line initializes a double array. There are 2 ways to initialize an array: new double[arrayLength] or new double[]{arrayContents}. The given code uses the second solution, so it creates a new double array with a single number (which is 0).

tibetiroka
  • 1,056
  • 2
  • 9
  • 17