0
0
Kotlinprogramming~10 mins

Type inference by the compiler in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type inference by the compiler
Start
Declare variable with initializer
Compiler analyzes initializer
Infer variable type
Assign inferred type to variable
Use variable with inferred type
End
The compiler looks at the value assigned to a variable and figures out its type automatically, so you don't have to write it.
Execution Sample
Kotlin
val number = 42
val text = "Hello"
val isActive = true
This code declares three variables and the compiler infers their types from the assigned values.
Execution Table
StepCode LineActionInferred TypeVariable Value
1val number = 42Compiler sees 42 (an integer)Int42
2val text = "Hello"Compiler sees "Hello" (a string)String"Hello"
3val isActive = trueCompiler sees true (a boolean)Booleantrue
4Use variablesVariables have inferred typesnumber: Int, text: String, isActive: Boolean42, "Hello", true
💡 All variables have their types inferred from their initial values.
Variable Tracker
VariableStartAfter InferenceFinal
numberuntypedIntInt
textuntypedStringString
isActiveuntypedBooleanBoolean
Key Moments - 2 Insights
Why don't we need to write the type explicitly?
Because the compiler looks at the value assigned (like 42 or "Hello") and automatically figures out the type, as shown in steps 1-3 of the execution table.
What happens if we try to assign a different type later?
The compiler will give an error because the variable's type is fixed after inference, as seen in step 4 where variables keep their inferred types.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what type does the compiler infer for the variable 'text' at step 2?
AString
BBoolean
CInt
DDouble
💡 Hint
Check the 'Inferred Type' column at step 2 in the execution table.
At which step does the compiler assign the type Boolean?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Inferred Type' column and find where 'Boolean' appears.
If we change 'val number = 42' to 'val number = 42.0', how would the inferred type change?
AIt would become String
BIt would stay Int
CIt would become Double
DIt would cause a compiler error
💡 Hint
Consider the type of 42.0 in Kotlin and how the compiler infers types from initial values.
Concept Snapshot
Type inference lets Kotlin figure out variable types from assigned values.
Use 'val' or 'var' with an initializer, no need to write the type.
Compiler checks the value and sets the variable's type automatically.
Once inferred, the variable's type cannot change.
Example: val x = 10 // x is Int
This makes code shorter and easier to read.
Full Transcript
In Kotlin, when you declare a variable with 'val' or 'var' and assign it a value, the compiler automatically figures out the variable's type by looking at the value. For example, if you write 'val number = 42', the compiler sees 42 is an integer and sets the type of 'number' to Int. This means you don't have to write 'val number: Int = 42' explicitly. The compiler does this for each variable with an initializer. Once the type is set, you cannot assign a different type to that variable later. This feature helps keep code clean and easy to write.