Utilize Control Flow in Kotlin

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

Lesson 04: Work with Null Values

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

Open up the starter project to get started.

  val input: String? = args.firstOrNull()
  val input: String = args.firstOrNull()
  val input: String = args.first()
  println("Input length: ${input.length}")
fun main() {
    val args = emptyArray<String>()
    val input: String = args.first()
}
    val nullableNumber = input?.toIntOrNull()
    println("Nullable number of students: $nullableNumber")
    val numberWithElvis = input?.toIntOrNull() ?: 0
    println("Number of students with elvis operator: $numberWithElvis")
    val numberWithCustomException = input?.toIntOrNull()
        ?: throw IllegalArgumentException("Invalid input, please enter a number")
    println("Number of students with custom exception: $numberWithCustomException")
    val nonNullableNumber = input!!.toInt()
    println("Non-nullable number of students: $nonNullableNumber")
    val safeCastNumber = nullableNumber as? Int
    println("Safe cast number: $safeCastNumber")
    val studentsInClassrooms = Arrays.asList(10, 20, null, 25)
  val studentsInClassrooms: MutableList<Int?> = Arrays.asList(10, 20, null, 25)
  val studentsInClassrooms: MutableList<Int> = Arrays.asList(10, 20, null, 25)
    val studentsInClassrooms: MutableList<Int> = Arrays.asList(10, 20, null, 25)
    println("Number of students in the second classroom:")
    println(studentsInClassrooms[1])
    println("Number of students in the third classroom:")
    println(studentsInClassrooms[2])
  val studentsInClassrooms: MutableList<Int?> = Arrays.asList(10, 20, null, 25)
  val studentsInClassrooms = Arrays.asList(10, 20, null, 25)
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion