← Kotlin tutorial

Kotlin

Variables & Data Types

val declares a value that can't be reassigned; var declares one that can.

Example: preferring val, using var only when needed

fun main() {
    val name = "Ada"
    var age = 21
    println("$name is $age years old.")
}
Ada is 21 years old.

Prefer val by default, the exact same recommendation the JavaScript tutorial gives for const over let, for the same reason: it prevents an accidental reassignment from compiling at all, catching a mistake immediately instead of it silently happening somewhere unexpected. Kotlin usually infers the type from the assigned value, though you can write it explicitly: val age: Int = 21.

Example
fun main() {
    val name = "Ada"
    var age = 21
    println("$name is $age years old.")
}
Output
Ada is 21 years old.
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account