Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Variables and Data Types  ›  Lesson

Type Inference

Type Inference 12 minKotlin works out a variable's type from the value you assign, so you write the type only when there is no initial value or when you want a different type than the one inferred.

Type Inference

Kotlin is strict about types: a number of cups is never confused with a customer name. Yet most Kotlin code barely mentions types at all. That is because the compiler reads the value you assign and works out the type itself.

The rule

The value names the type; write it only when there is no value.

val cups = 3                 // Int
val price = 3.5              // Double
val tip = 0.5f               // Float, because of the f
val beansSold = 3L           // Long, because of the L
val cafe = "Pebble Café"     // String
val isOpen = true            // Boolean

The type is fixed at compile time, even though you did not write it. Hover over cups in the IDE and it shows Int. Later, cups = "three" would still be an error (and cups is a val anyway).

When do you write the type?

  • No initial value. val openingHour: Int needs a type because there is nothing to infer from.
  • You want a different type than the default. val total: Long = 5 stores a small number as a Long.
  • Clarity at a boundary. Public functions often spell out their return type so readers do not have to guess.

Inference also works for expressions. val bill = cups * price is a Double, because an Int times a Double gives a Double. A function with an expression body infers its return type too:

fun double(n: Int) = n * 2   // returns Int

The shortcut

Read the literal's shape, left to right:

  • quotes "…" → String; single quotes '…' → Char
  • true / false → Boolean
  • a decimal point → Double, unless it ends in f → Float
  • a whole number → Int, unless it ends in L or is too big for Int → Long
Inferring the day's reportStep 1 of 4
  1. val cups = 42

    A whole number with no suffix that fits in 32 bits: Int.

A common surprise is val tip: Float = 0.5. The literal 0.5 is a Double, and Kotlin never converts it silently, so this fails. Write 0.5f.

Drills

Answer fast — trust the rule.

quick drill

What type does Kotlin infer for val price = 4.0?

quick drill

Which declaration is a compile error?

quick drill

Why does val openingHour (with nothing else on the line) fail to compile?

quick drill

val cups = 4 and val each = 2.5. What is the type of cups * each?

Remember

  • The literal's shape decides the type: quotes, point, suffix, size.
  • Write a type when there is no value or you want a non-default type.
  • 0.5 is Double; a Float needs 0.5f.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRead the compiler's mind

For the declarations val cups = 3, val price = 3.5, val tip = 0.5f, val beans = 12_000_000_000 and val name = "Ada", state the type Kotlin infers for each and explain the rule that decides it. Then give one case where you must write the type yourself.