Learn the Basics of the Kotlin Language

May 22 2024 · Kotlin 1.9, Android 14, Kotlin Playground 1.9

Lesson 03: Understand Types

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Kotlin has type inference. This means you don’t always have to be explicit about the type an object or variable has. Follow along with this demo by starting a new Kotlin Playground session, or any other Kotlin development environment of your choice. Here’s an example where a variable is created without specifying the type:

val amount = 200
val amount = 200
println(amount::class.simpleName)
val amount = 200f
println(amount::class.simpleName)
val amount = 200.0
println(amount::class.simpleName)
val amount: Float = 200f
println(amount::class.simpleName)
val amount: Double = 200f
println(amount::class.simpleName)

Checking Types

If you’re unsure of a variable’s type, you can use the is operator to check if the variable is of a particular type. Use it to check if “Bob” is a string:

val name: Any = "Bob"

val isString = name is String
println("Is name a String? " + isString)
val name: Any = "Bob"

if (name is String){
   println(name.uppercase())
}
val name: Any = "Bob"
val firstName = name as String
println(firstName::class.simpleName)
val amount: Any = 2
val result = amount as String
val amount: Any = 2
val result = amount as? String
println(result)

Strings

Strings are a common data type in most programs and programming languages. Strings represent text. In Kotlin, they’re made of a sequence of characters surrounded by double quotes. Being a sequence means you can iterate over the items that make up the sequence. Iterate over the following string:

val name: String = "Harry Potter"

for (char in name){
   println(char)
}
val fruits = "Apples" + " & " + "Oranges"
println(fruits)
val fruits = "Apples".plus(" & ").plus("Oranges")
println(fruits)

Kotlin String Interpolation and Templates

Kotlin has a special feature that evaluates code within a string. After evaluation, Kotlin converts the result to a string, if it isn’t one already. It then concatenates the evaluation result with the rest of the string. The result is added at the original location of the expression in the string literal.

val apples = "Apples"
val oranges = "Oranges"
val fruits = "$apples & $oranges"
println(fruits)
val apples = 2
val oranges = 3
val fruits = "There are ${apples + oranges} fruits in total."
println(fruits)
val amount = 25

val chat = """
   Ann: How much does this book cost?
   Bryan: This book costs $${amount}.
   And you get a discount of ${'$'}9.99
"""

println(chat)
val lowest = 45
val cash = 200
val books = 20

val chat = """
   Ann: With $$cash how many books can I afford?
   Bryan: ${ if (cash > lowest) 20 else 10 } books.
   Ann: ${
         if (books > 10) "Awesome!"
         else "That's OK!"
      }
"""

println(chat)
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Conclusion