How do I convert a `ByteArray` to `byte[]` so that...
# getting-started
k
How do I convert a
ByteArray
to
byte[]
so that I can pass it to a Java method that accepts the "traditional" byte arrays?
n
You shouldn't need to do any conversion. A ByteArray is Kotlin is represented as a
byte[]
on the JVM, so you can pass it where one is required. This works for me:
Copy code
import java.util.Arrays

val byteArray = ByteArray(10)
Arrays.fill(byteArray, 2)

for (byte in byteArray) {
    print(byte)
}
k
Ah, that makes sense. I thought it needed an explicit conversion.
e
Array<T> is Java T[] as well. which leads to the "fun" fact that, like Java,
Copy code
listOf(1)::class == listOf("a")::class // because erasure
arrayOf(1)::class != arrayOf("a")::class // because arrays of different element types have different representations