Concept Flow - Explicit type declaration
Start
Declare variable with type
Assign value matching type
Use variable
End
This flow shows declaring a variable with a specific type, assigning a matching value, then using it.
var name: String name = "Alice" var age: Int age = 30 println("Name: $name, Age: $age")
| Step | Action | Variable | Type | Value | Output |
|---|---|---|---|---|---|
| 1 | Declare 'name' with type String | name | String | uninitialized | |
| 2 | Assign "Alice" to 'name' | name | String | "Alice" | |
| 3 | Declare 'age' with type Int | age | Int | uninitialized | |
| 4 | Assign 30 to 'age' | age | Int | 30 | |
| 5 | Print values | Name: Alice, Age: 30 |
| Variable | Start | After Step 2 | After Step 4 | Final |
|---|---|---|---|---|
| name | uninitialized | "Alice" | "Alice" | "Alice" |
| age | uninitialized | uninitialized | 30 | 30 |
Explicit type declaration in Kotlin: val variableName: Type = value var variableName: Type = value - Declares variable with fixed type - Helps catch type errors early - Value must match declared type
val or var followed by variable name, colon, type, then assignment.var age: Int = 25. Others have syntax errors.val number: Int = 10 var text: String = "Hello" text = "Hi" println(number) println(text)
number is a val (fixed) with value 10. text is a var (changeable) initially "Hello", then changed to "Hi".number outputs 10. Printing text outputs the updated value "Hi".val count: Int = 5 count = 10
val means the variable is read-only and cannot be reassigned after initialization.count which is declared as val, causing an error.score that can change and must hold decimal numbers. Which explicit type declaration is correct in Kotlin?score must be changeable, so use var, not val.Double or Float, but value 95.5 is a Double by default.var and Double with correct value. Others either use val, wrong type, or wrong value type.Pass the test and this pattern moves unknown