Bird
Raised Fist0
Kotlinprogramming~10 mins

Explicit type declaration in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
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.
Execution Sample
Kotlin
var name: String
name = "Alice"
var age: Int
age = 30
println("Name: $name, Age: $age")
Declares variables with explicit types and prints their values.
Execution Table
StepActionVariableTypeValueOutput
1Declare 'name' with type StringnameStringuninitialized
2Assign "Alice" to 'name'nameString"Alice"
3Declare 'age' with type IntageIntuninitialized
4Assign 30 to 'age'ageInt30
5Print valuesName: Alice, Age: 30
💡 Program ends after printing the output.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
nameuninitialized"Alice""Alice""Alice"
ageuninitializeduninitialized3030
Key Moments - 2 Insights
Why do we write ': String' or ': Int' after variable names?
This tells Kotlin the exact type the variable must hold, so it only accepts matching values. See steps 1 and 3 in the execution table.
What happens if we assign a value of the wrong type?
Kotlin will give an error because the value does not match the declared type. This is why in steps 2 and 4, the assigned values match the declared types.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'name' after step 2?
Auninitialized
B"Alice"
C30
Dnull
💡 Hint
Check the 'Value' column for 'name' at step 2 in the execution table.
At which step is the variable 'age' assigned a value?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look for the assignment action for 'age' in the execution table.
If we remove ': Int' from 'age' declaration, what changes in the execution?
AKotlin infers the type automatically
BThe program will not compile
CThe variable 'age' becomes a String
DThe variable 'age' can hold any type
💡 Hint
Kotlin can infer types if explicit declaration is missing, so no error occurs.
Concept Snapshot
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
Full Transcript
This example shows how to declare variables in Kotlin with explicit types. First, we declare 'name' as a String and assign "Alice" to it. Then, we declare 'age' as an Int and assign 30. Finally, we print both values. Explicit type declaration means telling Kotlin exactly what type a variable holds, so it only accepts matching values. This helps avoid mistakes. The execution table shows each step: declaration, assignment, and printing. The variable tracker shows how values change. Remember, if you assign a wrong type, Kotlin will give an error. You can also let Kotlin guess the type by skipping explicit declaration, but here we show how to write it clearly.

Practice

(1/5)
1. What does explicit type declaration in Kotlin do?
easy
A. It automatically converts types without programmer input.
B. It tells Kotlin exactly what type a variable or value has.
C. It removes the need to use val or var.
D. It makes variables changeable by default.

Solution

  1. Step 1: Understand explicit type declaration

    Explicit type declaration means telling Kotlin the exact type of a variable, like Int or String.
  2. Step 2: Compare with other options

    The other options describe behaviors that are not related to explicit type declaration.
  3. Final Answer:

    It tells Kotlin exactly what type a variable or value has. -> Option B
  4. Quick Check:

    Explicit type declaration = telling exact type [OK]
Hint: Explicit means you say the type clearly [OK]
Common Mistakes:
  • Thinking Kotlin guesses types without declaration
  • Confusing explicit type with variable mutability
  • Believing explicit type removes need for val/var
2. Which of the following is the correct syntax for explicit type declaration of a variable in Kotlin?
easy
A. var age: Int = 25
B. val height = Int 170
C. val name String = "Alice"
D. var weight = 70: Int

Solution

  1. Step 1: Recall Kotlin variable declaration syntax

    Kotlin uses val or var followed by variable name, colon, type, then assignment.
  2. Step 2: Check each option

    var age: Int = 25 matches this syntax: var age: Int = 25. Others have syntax errors.
  3. Final Answer:

    var age: Int = 25 -> Option A
  4. Quick Check:

    Correct syntax uses colon before type [OK]
Hint: Use colon (:) before type in declaration [OK]
Common Mistakes:
  • Omitting colon before type
  • Placing type after value
  • Using wrong order of keywords
3. What will be the output of this Kotlin code?
val number: Int = 10
var text: String = "Hello"
text = "Hi"
println(number)
println(text)
medium
A. Error: Cannot reassign val
B. 10\nHello
C. 10\nHi
D. 10\nnull

Solution

  1. Step 1: Analyze variable declarations and assignments

    number is a val (fixed) with value 10. text is a var (changeable) initially "Hello", then changed to "Hi".
  2. Step 2: Determine printed output

    Printing number outputs 10. Printing text outputs the updated value "Hi".
  3. Final Answer:

    10 Hi -> Option C
  4. Quick Check:

    val fixed, var changeable = 10 and Hi [OK]
Hint: val is fixed, var can change value [OK]
Common Mistakes:
  • Thinking val can be reassigned
  • Confusing initial and updated values
  • Expecting syntax error for reassignment of var
4. Identify the error in this Kotlin code snippet:
val count: Int = 5
count = 10
medium
A. Missing type declaration for count
B. Variable count should be declared with var
C. Incorrect type Int assigned to count
D. Cannot reassign a value to val variable

Solution

  1. Step 1: Understand val and var difference

    val means the variable is read-only and cannot be reassigned after initialization.
  2. Step 2: Check the reassignment

    The code tries to assign 10 to count which is declared as val, causing an error.
  3. Final Answer:

    Cannot reassign a value to val variable -> Option D
  4. Quick Check:

    val is fixed, reassignment causes error [OK]
Hint: val variables cannot be reassigned [OK]
Common Mistakes:
  • Thinking val variables can be changed
  • Confusing val and var keywords
  • Ignoring reassignment rules
5. You want to declare a variable score that can change and must hold decimal numbers. Which explicit type declaration is correct in Kotlin?
hard
A. var score: Double = 95.5
B. var score: Int = 95.5
C. val score: Double = 95.5
D. val score: Float = "95.5"

Solution

  1. Step 1: Identify mutability requirement

    The variable score must be changeable, so use var, not val.
  2. Step 2: Choose correct type for decimal numbers

    Decimal numbers require Double or Float, but value 95.5 is a Double by default.
  3. Step 3: Check options

    var score: Double = 95.5 uses var and Double with correct value. Others either use val, wrong type, or wrong value type.
  4. Final Answer:

    var score: Double = 95.5 -> Option A
  5. Quick Check:

    var + Double for changeable decimal [OK]
Hint: Use var for changeable, Double for decimals [OK]
Common Mistakes:
  • Using val when variable should change
  • Assigning decimal to Int type
  • Putting string value for numeric type
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard