Bird
Raised Fist0
Kotlinprogramming~10 mins

Any type as universal base in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Any type as universal base
Start
↓
Declare variable of type Any
↓
Assign any value: Int, String, Boolean, etc.
↓
Use variable (print, check type, cast)
↓
End
In Kotlin, Any is the universal base type. You can store any value in a variable of type Any and then use or cast it as needed.
Execution Sample
Kotlin
fun main() {
  val x: Any = 42
  println(x)
  val y: Any = "Hello"
  println(y)
}
This code shows variables of type Any holding an Int and a String, then printing them.
Execution Table
StepActionVariableValueOutput
1Declare x as Any and assign 42x42 (Int)
2Print xx42 (Int)42
3Declare y as Any and assign "Hello"y"Hello" (String)
4Print yy"Hello" (String)Hello
5End of program
💡 Program ends after printing both variables.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
xuninitialized42 (Int)42 (Int)42 (Int)
yuninitializeduninitialized"Hello" (String)"Hello" (String)
Key Moments - 2 Insights
Why can variable x hold an Int and variable y hold a String even though both are declared as Any?
Because Any is the universal base type in Kotlin, it can hold any type of value like Int, String, Boolean, etc. This is shown in execution_table rows 1 and 3.
What happens if you try to use x as an Int directly without casting?
You cannot use x as an Int directly because its type is Any. You need to cast it to Int first. This is implied by the variable type in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of variable x after step 1?
A42 (Int)
B"Hello" (String)
Cuninitialized
Dnull
💡 Hint
Check the 'Value' column for variable x at step 1 in the execution_table.
At which step does variable y get assigned a value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table to see when y is assigned.
If you want to use variable x as an Int, what must you do?
AUse it directly as Int
BDeclare x as String
CCast x to Int
DAssign x a Boolean value
💡 Hint
Refer to key_moments about type casting from Any to specific types.
Concept Snapshot
Any is Kotlin's universal base type.
Variables of type Any can hold any value: Int, String, Boolean, etc.
You must cast Any to a specific type to use type-specific operations.
Use Any when you want a variable to hold any kind of data.
Printing Any variables shows their actual value.
Full Transcript
In Kotlin, the Any type is the universal base type. This means you can declare a variable of type Any and assign it any value, such as an integer or a string. For example, a variable x of type Any can hold the integer 42, and a variable y of type Any can hold the string "Hello". When you print these variables, you see their actual values. However, to use the variable as a specific type, like Int, you need to cast it from Any to that type. This flexibility makes Any useful when you want a variable to hold any kind of data.

Practice

(1/5)
1. In Kotlin, which type is the universal base type that can hold any kind of value?
easy
A. Universal
B. Object
C. Base
D. Any

Solution

  1. Step 1: Understand Kotlin's type hierarchy

    Kotlin uses Any as the root of its type system, similar to Object in Java.
  2. Step 2: Identify the universal base type

    Any can hold any non-null value, making it the universal base type in Kotlin.
  3. Final Answer:

    Any -> Option D
  4. Quick Check:

    Universal base type = Any [OK]
Hint: Remember: Kotlin's root type is called Any [OK]
Common Mistakes:
  • Confusing Any with Object (Java's base type)
  • Thinking Base or Universal are Kotlin types
  • Assuming Any can hold null (it cannot)
2. Which of the following is the correct way to declare a variable of type Any in Kotlin?
easy
A. val x: Any = 10
B. val x = Any(10)
C. val x: Any() = 10
D. val x: any = 10

Solution

  1. Step 1: Review Kotlin variable declaration syntax

    Variables are declared with val or var, followed by a colon and the type.
  2. Step 2: Check correct usage of Any type

    Any is a type, not a constructor, so you assign values directly without parentheses.
  3. Final Answer:

    val x: Any = 10 -> Option A
  4. Quick Check:

    Declare with colon and type name = val x: Any = 10 [OK]
Hint: Use colon for type, no parentheses for Any [OK]
Common Mistakes:
  • Trying to call Any as a constructor
  • Using lowercase 'any' instead of 'Any'
  • Adding parentheses after type name
3. What will be the output of this Kotlin code?
fun main() {
    val a: Any = "Hello"
    val b: Any = 123
    println(a)
    println(b)
}
medium
A. Hello 123
B. Any Any
C. Compilation error
D. null null

Solution

  1. Step 1: Understand variable assignments

    Variable a holds a string "Hello" and b holds an integer 123, both typed as Any.
  2. Step 2: Analyze the print statements

    Printing a and b calls their toString() method, outputting "Hello" and "123" respectively.
  3. Final Answer:

    Hello 123 -> Option A
  4. Quick Check:

    Any holds values, prints their string form [OK]
Hint: Any prints actual value's string, not type name [OK]
Common Mistakes:
  • Expecting type name 'Any' to print
  • Thinking it causes compile error
  • Assuming null prints for Any
4. Identify the error in this Kotlin code snippet:
fun main() {
    val x: Any = null
    println(x)
}
medium
A. Missing semicolon after println
B. println cannot print Any type
C. Cannot assign null to a variable of type Any
D. Variable x must be declared as var, not val

Solution

  1. Step 1: Check type nullability

    Any in Kotlin is non-nullable by default, so it cannot hold null.
  2. Step 2: Identify assignment error

    Assigning null to x of type Any causes a compile-time error.
  3. Final Answer:

    Cannot assign null to a variable of type Any -> Option C
  4. Quick Check:

    Any is non-nullable, null assignment fails [OK]
Hint: Any disallows null; use Any? for nullable [OK]
Common Mistakes:
  • Thinking println can't print Any
  • Expecting semicolons in Kotlin
  • Confusing val and var for nullability
5. Given a list of mixed types, how can you create a new list containing only the string representations of elements using Any as the base type?
val mixedList: List<Any> = listOf(1, "two", 3.0, true)

Which Kotlin code correctly produces List<String> with string forms of all elements?
hard
A. val stringList = mixedList.filterIsInstance<String>()
B. val stringList = mixedList.map { it.toString() }
C. val stringList = mixedList.map { it as String }
D. val stringList = mixedList.filter { it is String }

Solution

  1. Step 1: Understand the goal

    We want a list of strings representing all elements, not just the strings themselves.
  2. Step 2: Analyze each option

    val stringList = mixedList.map { it.toString() } maps each element to its string form, producing List<String>. val stringList = mixedList.filterIsInstance<String>() filters only strings, losing other elements. val stringList = mixedList.map { it as String } tries unsafe cast causing runtime error. val stringList = mixedList.filter { it is String } filters strings but returns List<Any>, not List<String>.
  3. Final Answer:

    val stringList = mixedList.map { it.toString() } -> Option B
  4. Quick Check:

    Map all to string form for List<String> [OK]
Hint: Use map with toString() to convert all elements [OK]
Common Mistakes:
  • Using filterIsInstance<String> loses non-string elements
  • Casting with 'as String' causes runtime errors
  • Filtering returns List<Any>, not List<String>
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