Phase 1 · FoundationsModule 2~34 min read

Variables, Types & Basic Syntax

Store data with val and var, Kotlin's type system, and readable string templates.

What you'll learn

Every program stores and manipulates data. This module covers how Kotlin holds values in variables, its type system, and the string templates that make Kotlin code so readable.

By the end you'll be able to:

  • Declare variables with val and var — and know which to prefer
  • Use Kotlin's basic types and let the compiler infer them
  • Build strings cleanly with string templates
  • Convert between types explicitly
  • Define compile-time constants with const val

val vs var

Kotlin has two ways to declare a variable, and choosing between them is a real design decision. val creates a read-only reference (assigned once); var creates a mutable one. Notice there's no type keyword and no new — you just name it and assign:

Read-only vs mutable

val — read-only

Assigned once, never reassigned. Kotlin's default — prefer it for safer, more predictable code.

var — mutable

Can be reassigned. Use only when the value genuinely needs to change.

Variables.kt
val name = "Sara"      // read-only
var age = 25           // mutable

age = 26               // OK — var can change
// name = "Omar"       // ERROR — val cannot be reassigned

println(name)
println(age)

Prefer val

A strong Kotlin habit: reach for val by default, and only switch to var when you truly need to reassign. Immutable values are easier to reason about and prevent a whole class of bugs.

Types & inference

Kotlin is statically typed — every value has a type known at compile time — but you rarely need to write the type out, because the compiler infers it from the value. You can state it explicitly with val name: Type when it aids clarity:

Types.kt
val count: Int = 42          // explicit type
val price = 19.99            // inferred as Double
val letter = 'K'             // Char
val isReady = true           // Boolean
val big = 1_000_000L         // Long (note the L)

println(count + big)
TypeHoldsExample
Intwhole numbers (the default)val n = 42
Longlarge whole numbersval n = 42L
Doubledecimals (the default)val p = 3.14
Floatsmaller decimalsval p = 3.14f
Chara single characterval c = 'K'
Booleantrue or falseval ok = true
Stringtextval s = "hi"

Note

In Kotlin, everything is an object — even numbers. There are no Java-style primitives to worry about, though the compiler optimises them to primitives behind the scenes.

String templates

Building text from values is a joy in Kotlin. A string template uses $ to insert a variable, and ${...} to insert any expression:

Templates.kt
val name = "Sara"
val age = 25

println("Name: $name")                     // simple variable
println("Next year: ${age + 1}")          // an expression needs braces
println("Uppercase: ${name.uppercase()}") // a method call

Tip

Use the simple $name form for a bare variable, and the ${...} form whenever you need an expression, a method call, or a property access. It's cleaner than concatenating with +.

Type conversion

Unlike Java, Kotlin does not automatically convert between number types — you must be explicit with methods like toInt(), toDouble(), or toString(). This prevents subtle bugs from surprise conversions:

Convert.kt
val text = "42"
val number = text.toInt()      // String -> Int
println(number + 8)            // 50

val x = 10
val y = 3.0
println(x.toDouble() / y)      // explicit conversion required

Constants

For a value that's a genuine compile-time constant (known before the program runs, at the top level or in a companion object), use const val. By convention, constants are named in UPPER_SNAKE_CASE:

Constants.kt
const val MAX_USERS = 100
const val APP_NAME = "MasterCoding"

Note

A regular val can be set at runtime; a const val must be known at compile time (so it can only hold primitives and strings). Reach for const for fixed configuration values.

Recap & quick check

Key takeaways

  • val is read-only (preferred); var is mutable — reach for val by default.
  • Kotlin is statically typed but infers types, so you rarely write them out.
  • Everything is an object; Int, Double, Boolean, Char, String are the everyday types.
  • String templates: $name for a variable, ${...} for any expression.
  • Kotlin never auto-converts number types — use toInt(), toDouble(), etc. explicitly.

Quick check

1. Which keyword declares a read-only variable?

2. Do you have to write the type when declaring a variable?

3. How do you insert an expression into a string?

4. How do you convert a Double to an Int in Kotlin?

5. Which should you prefer by default?

Great — you can store and shape data now. Next up: Module 3 — Null Safety, Kotlin's most celebrated feature.