Scala: are the initial values ​​in the [Double] array safe to use?

If an Array [Double] array containing all zeros is required, is it safe to use

    val allZeros = new Array[Double](10)
    val whatever = allZeros( 5 )     // guaranteed to be 0.0, not null?
    assert( whatever == 0.0 )        // succeeds

or should i stick

    val allZeros = Array.fill[Double](10)( 0.0 )

I know that the first version works, but is this a guarantee that the language makes, i.e. will she always be safe? A double can theoretically also be initialized null(although thinking about it as a language developer, I would prefer not to make such a change :-).

+5
source share
2 answers

The double in Scala is not a type object java.lang.Double, but a primitive type double. Thus, the default value is 0. You can use your first version, which is completely safe.

, : .

+5

, . Java, Scala.

+3

All Articles