100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Variables and Data Types in Kotlin

Learn how Kotlin declares variables with val and var, and the built-in data types used to store values.

BasicsBeginner8 min readJul 8, 2026
Analogies

Introduction

Every Kotlin program works with data, and that data lives in variables. Kotlin gives you two keywords for declaring variables: val for read-only (immutable) references and var for mutable ones. Kotlin is statically typed, meaning every variable has a type known at compile time, but thanks to type inference you rarely have to write the type explicitly. Kotlin also ships with a rich set of built-in data types for numbers, text, and boolean logic.

🏏

Cricket analogy: A player's fixed jersey number is like a val, set once and never reassigned, while the live run count during an innings behaves like a var, updated ball by ball as Kotlin infers its type as Int automatically.

Syntax

kotlin
val readOnlyName: String = "Ada"
var mutableCount: Int = 0

// Type inference lets you omit the type
val inferredName = "Ada"
var inferredCount = 0

Explanation

A val is similar to Java's final field: once assigned, the reference cannot be reassigned to point at a different value. A var can be reassigned as many times as needed, as long as the new value matches the declared type. Kotlin's compiler infers the type from the assigned value, so val x = 5 makes x an Int without you writing Int explicitly. Kotlin's basic data types include Int, Long, Short, Byte, Double, Float, Boolean, Char, and String. Unlike Java, Kotlin has no separate 'primitive' types at the language level — everything is treated as an object — though the compiler optimizes most of these to JVM primitives under the hood for performance.

🏏

Cricket analogy: A batter's date of debut, once recorded, is like Java's final field: you can't reassign it, matching val. Kotlin infers val centuries = 5 as Int, and though every stat is technically an object, the scoreboard engine optimizes counts to raw numbers for speed.

Example

kotlin
fun main() {
    val name: String = "Kotlin"
    var age: Int = 10
    val pi: Double = 3.14159
    val isFun: Boolean = true
    val grade: Char = 'A'

    age = 11 // allowed, age is a var

    println("$name is $age years old, pi is $pi, fun=$isFun, grade=$grade")
}

Output

text
Kotlin is 11 years old, pi is 3.14159, fun=true, grade=A

Key Takeaways

  • Use val for read-only references and var for mutable ones; prefer val whenever possible.
  • Kotlin infers types automatically, but you can declare them explicitly with a colon, e.g. val x: Int = 5.
  • Core data types include Int, Long, Short, Byte, Double, Float, Boolean, Char, and String.
  • Kotlin treats all types as objects at the language level, but the compiler optimizes to JVM primitives where possible.
  • A val's reference cannot be reassigned, but if it holds a mutable object, that object's internal state can still change.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#VariablesAndDataTypesInKotlin#Variables#Data#Types#Syntax#StudyNotes#SkillVeris