Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- marp: true
- theme: uncover
- class: invert
- ---
- <style>
- p {
- font-size: 30px;
- }
- </style>
- # Kotlin Null safety
- ---
- ## Nullable և Non-Null հղումներ
- **Nullable**
- ```kotlin
- var nullableText: String? = "abc"
- nullableText = null
- ```
- **Non-Null**
- ```kotlin
- var nonNullText: String = "abc"
- nonNullText = null // կոմպիլյացիայի սխալ
- ```
- ```kotlin
- var nonNullText: String = "abc"
- nonNullText = nullableText // կոմպիլյացիայի սխալ
- ```
- ---
- ## Smart casts
- ```kotlin
- val x = readln().toIntOrNull()
- val y = readln().toIntOrNull()
- if (x != null && y != null) {
- // x: String? -> x: String
- // y: String? -> y: String
- println(x * y)
- }
- else {
- println("'x' or 'y' is not a number")
- }
- ```
- ...
- ---
- ...
- ```kotlin
- val x = readln().toIntOrNull()
- val y = readln().toIntOrNull()
- if (x == null || y == null) {
- println("'x' or 'y' is not a number")
- return
- }
- // x: String? -> x: String
- // y: String? -> y: String
- println(x * y)
- ```
- ---
Advertisement
Add Comment
Please, Sign In to add comment