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
val readOnlyName: String = "Ada"
var mutableCount: Int = 0
// Type inference lets you omit the type
val inferredName = "Ada"
var inferredCount = 0Explanation
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
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
Kotlin is 11 years old, pi is 3.14159, fun=true, grade=AKey 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
1. Which keyword declares a read-only variable in Kotlin?
2. What does Kotlin do when you write val x = 5 without a type annotation?
3. Which of these is NOT a built-in Kotlin data type?
4. What happens if you try to reassign a val after it has been initialized?
5. Which statement about Kotlin variable declarations is correct?
Was this page helpful?
You May Also Like
Operators in Kotlin
Explore Kotlin's arithmetic, comparison, logical, range, and containment operators, including the == vs === distinction.
Type Conversion in Kotlin
Understand why Kotlin requires explicit conversion between numeric types and how to use functions like toInt() and toDouble().
Null Safety in Kotlin
Kotlin's type system distinguishes nullable from non-nullable types at compile time, eliminating most NullPointerExceptions before code ever runs.
Introduction to Kotlin Programming
A beginner-friendly overview of Kotlin, a modern statically typed language that runs on the JVM and interoperates fully with Java.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics