NarekNavoyan

Untitled

Aug 25th, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. ---
  2. marp: true
  3. theme: uncover
  4. class: invert
  5. ---
  6.  
  7. <style>
  8. p {
  9. font-size: 30px;
  10. }
  11. </style>
  12.  
  13.  
  14. # Kotlin Null safety
  15.  
  16.  
  17. ---
  18.  
  19.  
  20. ## Nullable և Non-Null հղումներ
  21.  
  22. **Nullable**
  23. ```kotlin
  24. var nullableText: String? = "abc"
  25. nullableText = null
  26. ```
  27.  
  28. **Non-Null**
  29. ```kotlin
  30. var nonNullText: String = "abc"
  31. nonNullText = null // կոմպիլյացիայի սխալ
  32. ```
  33.  
  34. ```kotlin
  35. var nonNullText: String = "abc"
  36. nonNullText = nullableText // կոմպիլյացիայի սխալ
  37. ```
  38.  
  39.  
  40. ---
  41.  
  42.  
  43. ## Smart casts
  44.  
  45. ```kotlin
  46. val x = readln().toIntOrNull()
  47. val y = readln().toIntOrNull()
  48.  
  49. if (x != null && y != null) {
  50. // x: String? -> x: String
  51. // y: String? -> y: String
  52. println(x * y)
  53. }
  54. else {
  55. println("'x' or 'y' is not a number")
  56. }
  57. ```
  58.  
  59. ...
  60.  
  61.  
  62. ---
  63.  
  64.  
  65. ...
  66.  
  67. ```kotlin
  68. val x = readln().toIntOrNull()
  69. val y = readln().toIntOrNull()
  70.  
  71. if (x == null || y == null) {
  72. println("'x' or 'y' is not a number")
  73. return
  74. }
  75. // x: String? -> x: String
  76. // y: String? -> y: String
  77.  
  78. println(x * y)
  79. ```
  80.  
  81.  
  82. ---
  83.  
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment