Programming in Kotlin: Fundamentals

Aug 9 2022 Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 3: Functions & Nullability

20. Create & Consume Nullables

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions
See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 19. Introduction Next episode: 21. Challenge: Use Nullables

This video was last updated on Aug 9 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

So far, all the constants and variables you’ve used and created had values. However, in programming, that isn’t always the case.

// Starter code
val myName = "Emmanuel"
val nickname: String

val myName = "Emmanuel"
val nickname: String?
val myName = "Emmanuel"
val nickname: String? = null

println(nickname)
val myName = "Emmanuel"
val nickname: String? = null
val lastName: String = null
val myName = "Emmanuel"
val nickname: String? = null
val lastName: String? = null
val nicknameLength = nickname.length
val nicknameLength = nickname!!.length
println(nicknameLength)
val nicknameLength = nickname?.length
println(nicknameLength)
val nicknameLength = nickname?.length?.toDouble()
println(nicknameLength)
val nickname: String? = "Stretchy"
if (lastName != null) {
  println("My last name is ${lastName.length} characters long!")
} else {
  println("I don't have a last name!")
}
if (nickname?.isEmpty()) {
  println("You don't have a nickname! It's length is: ${nickname.length}")
}
val myNickname = nickname ?: myName
println(myNickname)
val myName = "Emmanuel"
val nickname: String? = null
val lastName: String? = null