Scala Language Java Interoperability Arrays


Arrays are regular JVM arrays with a twist that they are treated as invariant and have special constructors and implicit conversions. Construct them without the new keyword.

 val a = Array("element")

Now a has type Array[String].

 val acs: Array[CharSequence] = a
 //Error: type mismatch;  found   : Array[String]  required: Array[CharSequence]

Although String is convertible to CharSequence, Array[String] is not convertible to Array[CharSequence].

You can use an Array like other collections, thanks to an implicit conversion to TraversableLike ArrayOps:

 val b: Array[Int] =

Most of the Scala collections (TraversableOnce) have a toArray method taking an implicit ClassTag to construct the result array:

 //> res1: Array[Int] = Array(0)

This makes it easy to use any TraversableOnce in your Scala code and then pass it to Java code which expects an array.