HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to KotlinVariables (val / var)Data TypesStringsListsConditions (if / when)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Kotlin›Variables (val / var)

Variables (val / var)

Holding immutable values with val and mutable values with var; type inference and naming conventions.

Simple enough for a 5-year-old to understand

val: immutable value

You cannot reassign a new value to a value defined with val later. val is preferred first because it is safe.

Example code
1
val name = "Alex"
Text value; its type automatically becomes String.
VariableText value; its type automatically becomes String.
2
println(name)
Alex writes.
Output / InputAlex writes.
VariableOutput / Input
💡 Hint: val is like a constant: assign once, do not change later.

var: mutable value

Example code
1
var score = 10
Numeric variable.
VariableNumeric variable.
2
score = 20
It can be modified because it is var.
VariableIt can be modified because it is var.
3
println(score)
Writes 20.
Output / InputWrites 20.
VariableOutput / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which one do you use for a definition whose value will not change later?

About this topic

There are two ways to store values in Kotlin: val and var. A name defined with val receives a value once and cannot be changed; a name defined with var can be reassigned later. This distinction is one of the language's fundamental habits.

The community's common recommendation is to use val by default. An unchanging value reduces things the person reading the code has to keep track of in their mind; additionally, in concurrently running code, immutability eliminates an entire class of bugs.

Kotlin often infers the type from the initial value. When you write val count = 5, the type becomes Int. If you want to explicitly write the type, you use a colon; this is mandatory if you are going to assign the value later.

What you will learn

  • the difference between val and var
  • Type inference and explicit type declaration
  • nullable types
  • Naming conventions
  • why immutability is preferred

Who is this for?

  • beginners to Kotlin
  • those switching from Java who are looking for the final keyword
  • those who want to learn null safety

When to use?

Everything that remains constant throughout the flow, such as a calculation result, configuration value, and data coming into a function, should be val.

Values that actually change, such as a loop counter, accumulated total, and screen state, should be var.

Common mistakes

Wrong

val total = 10
total = 20

A name defined with val cannot be reassigned; the compiler gives a "Val cannot be reassigned" error. This is the equivalent of the final keyword in Java, but in Kotlin, it is used as the default behavior. If you are going to change the value, you must write var.

Correct

var total = 10
total = 20

Wrong

var name: String = null

In Kotlin, normal types do not accept null values; this code gives a "Null can not be a value of a non-null type String" error. To define a variable that can be null, you must put a question mark at the end of the type. This rule is Kotlin's most important safety feature.

Correct

var name: String? = null

Wrong

var score
score = 10

Since there is no initial value, the compiler cannot infer the type and gives a "This variable must either have a type annotation or be initialized" error. If you are going to assign the value later, you need to explicitly declare the type.

Correct

var score: Int
score = 10

Frequently asked questions

Are val and Java's final the same thing?

In terms of preventing reassignment, yes. However, val does not guarantee that the content of the object it points to will not change: an element can be added to a mutable list defined with val. If you want immutability, you must use both val and an immutable collection type.

Should I always write the type?

No. If there is an initial value, type inference is sufficient and keeps the code readable. General rule: do not write the type for local variables, write it in public function signatures — there, the type is part of the documentation.

What is lateinit used for?

It is for situations where you cannot provide the value at definition time but know it will definitely be assigned before it is used; this way, you don't have to make the type nullable. The risk is this: if accessed without being assigned, you get a runtime error, meaning you guarantee the safety.

Summary

val cannot be reassigned, var can be assigned.

The default choice should be val.

Normal types do not accept null values.

A nullable type takes a question mark at the end.

If there is no initial value, the type must be written.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code