Kotlin byteArrayOf()

Kotlin byteArrayOf()

The byteArrayOf() function in Kotlin is used to create an array of Byte with the specified values. It provides a concise way to initialize a byte array.

In this tutorial, we shall learn the syntax and go through some examples of Kotlin byteArrayOf() function.

Syntax

The syntax of the byteArrayOf() function is:

fun byteArrayOf(vararg elements: Byte): ByteArray

where

ParameterDescription
elementsThe values to be included in the byte array.
Parameter of byteArrayOf() function

The function returns a byte array containing the specified elements.

Examples

1. Using byteArrayOf() to Create a Byte Array

In this example, we’ll use byteArrayOf() to create a byte array with specified values.

Kotlin Program

fun main() {
    val byteArray = byteArrayOf(1, 2, 3, 4, 5)
    
    // Display the created byte array
    println("Created Byte Array: ${byteArray.joinToString(", ")}")
}

Output

Created Byte Array: 1, 2, 3, 4, 5

In this example, byteArrayOf() is used to create a byte array with specified values, and the array is displayed using joinToString().

Summary

The byteArrayOf() function in Kotlin is a convenient way to initialize a byte array with specified values. We have seen how to use byteArrayOf() function to create a Byte array from given values.