to fix this var username String null if username null val uppercase

To fix this var username string null if username null

This preview shows page 18 - 31 out of 69 pages.

to fix this: var username: String? = null if (username != null) { val uppercase = username.toUpperCase() } 18
Image of page 18
19
Image of page 19
The Elvis Operator The Elvis operator can be used with nullable types to define a default value to be returned when a value or expression is null. Example: var s: String? if (s != null) { return s } else { return "String is null" } Use the Elvis operator instead: return s ?: "String is null" 20
Image of page 20
Not-Null Assertion The not-null assertion removes all of the compiler restrictions from a nullable type. val len = username !! .length The code will crash with the message " Exception in thread "main" kotlin.KotlinNullPointerException " if username is null. Use of the not-null assertion is discouraged and should only be used in situations where you are certain that the value will not be null. 21
Image of page 21
Type Casting Sometimes it may be necessary to type cast an object. Unsafe cast operator in Kotlin: as Example: val tv = findViewById(R.id.aview) as TextView Safe cast operator in Kotlin: as? returns null if the cast operator can not be performed. 22
Image of page 22
Kotlin Operators Kotlin provides a range of operators. Arithmetic operators: + , -, * , /, % Assignment operators: +=, -=, *=, /=, %= Increment and decrement operators: ++, -- Equality operators: ==, != Relational operators: >, >=, <, <= Logic operators: !, &&, !! Range operator: .. e.g.,: 5..8 specifies the numbers 5, 6, 7 and 8 23
Image of page 23
Kotlin Flow Control Conditional flow control if, if/else, when Looping flow control for-in, while, do..while break, continue 24
Image of page 24
Conditional Flow Control Syntax of the if expression if (boolean expression) { // Kotlin code to be executed if expression is true } Syntax of the if expression if (boolean expression) { // Kotlin code to be executed if expression is true } else { // Kotlin code to be executed if expression is false } Braces {} are optional if only one line of code to be executed. 25
Image of page 25
Conditional Flow Control Note that if constructs are expressions, not statements as in other programming languages. There is no ternary operator (condition ? then : else) in Kotlin. Example: if (x > y) max = x else max = y can be written in Kotlin as follows: max = if ( x> y) x else y 26
Image of page 26
Conditional Flow Control If there is more than one line of a code in if branches, then the last expression is returned. Example: val max = if (x > y) { print("x is greater than y") x } else { print("x is not greater than y") y } 27
Image of page 27
Conditional Flow Control Syntax of the when expression: when (value) { match1 -> // code to be executed match2 -> // code to be executed else -> // default code to be executed } 28
Image of page 28
Conditional Flow Control Example: var a = 4 when (a) { 1 -> println("a is 1") 2 -> println("a is 2") 3, 4 -> {println ("a is 3"); println ("or a is 4")} in 5.. 10 -> println("a is in 5..10") else -> println("a is greater than 10") } 29
Image of page 29
Conditional Flow Control Syntax of the for-in loop: for (variable in collection or range) {
Image of page 30
Image of page 31

You've reached the end of your free preview.

Want to read all 69 pages?

  • Spring '06
  • J.Rajnovich
  • var, Kotlin

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture