Reputation: 176
Why does an array of type Int have its entries initialised to 0, but an array of type BigInt have its entries initialised to null?
val a = new Array[Int](1)
val b = new Array[BigInt](1)
println(a.mkString())
println(b.mkString())
yields
0
null
Upvotes: 4
Views: 252
Reputation: 97331
According to the scaladoc for Int
:
Instances of Int are not represented by an object in the underlying runtime system.
Looking at the compiled class file, it indeed shows that the array of Int
s becomes an array of int primitives in the bytecode. And int primitives have a value of 0 by default.
0 iconst_1
1 newarray int [10]
3 astore_2 [a]
4 iconst_1
5 anewarray scala.math.BigInt [16]
Upvotes: 5