https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Kirill Grouchnikov

08/26/2021, 7:39 PM
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

nschulzke

08/26/2021, 7:43 PM
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

Kirill Grouchnikov

08/26/2021, 7:46 PM
Ah, that makes sense. I thought it needed an explicit conversion.
e

ephemient

08/26/2021, 8:00 PM
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
11 Views