bhylu.com

Advertisements

Kotlin Arrays

Use an array for a fixed set of service positions.

How it works

Array stores a fixed number of mutable elements. Array<Int> and IntArray are different types; IntArray avoids boxed integer elements on the JVM. Arrays use reference equality with ==, so use contentEquals when you mean to compare their elements.

Run this Kotlin example

These are local Kotlin/JVM programs. Install a JDK and the compiler using the official Kotlin compiler guide, or use an IDE with Kotlin support. Check java -version and kotlinc -version in a new terminal. The reviewed compiler is Kotlin 2.4.21 with a JVM 21 target, executed on Temurin JDK 25.0.5. Use JDK 21 or newer for this target. Android, Kotlin/JS and Kotlin/Native require separate platform projects and are not the tested environment here.

Save the complete source as arrays.kt in its own learning folder. The package line gives this example a namespace, not a network address. Bhylu has no Kotlin browser runner; do not use the JavaScript or Python editor. Apache, PHP and MySQL are not required.

Compile with kotlinc arrays.kt -jvm-target 21 -include-runtime -d lesson.jar, then run java -jar lesson.jar. Use a learning folder you own and keep generated JARs outside the website. These sources require no third-party application libraries.

Download the complete arrays.kt · Download all source files with hash checks

For the optional collection, save download-examples.mjs in a new learning folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/kotlin/. It refuses an existing bhylu-kotlin-examples folder and installs or executes no Kotlin code. Compile one ordinary lesson at a time by filename.

Example

arrays.kt

package bhylu.lessons.k_arrays

fun main() {
    val slots = intArrayOf(2, 0, 4)
    val copy = slots.copyOf()
    copy[0] = 9
    println(slots.joinToString())
    println(slots.contentEquals(intArrayOf(2, 0, 4)))
}

Expected result

2, 0, 4
true

copyOf creates a separate integer array, so the original remains unchanged. Copying arrays of object references does not deep-copy the referenced objects. Bounds checks still apply to every indexed access.

For exact language and library contracts, consult the official Kotlin documentation. These fictional fixtures are learning programs, not a deployed hire service.

Advertisements