Advertisement
Sweetening

Kotlin Cheat Sheet

Feb 8th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. Below is a concise Kotlin cheat sheet covering the basics, including syntax for variable declaration, control flows, functions, classes, and more. Kotlin is a statically typed programming language that runs on the Java Virtual Machine and can be used for developing Android apps, server-side applications, and much more.
  2.  
  3. Variables
  4. val immutableVariable: String = "I cannot be reassigned"
  5. var mutableVariable: Int = 42
  6. // Type inference
  7. val inferredType = "The compiler knows this is a String"
  8.  
  9. Basic Types
  10. val myString: String = "Hello, Kotlin!"
  11. val myInt: Int = 123
  12. val myDouble: Double = 123.45
  13. val myBoolean: Boolean = true
  14. val myLong: Long = 123L
  15. val myFloat: Float = 123.45F
  16. val myChar: Char = 'A'
  17.  
  18. Arrays and Collections
  19. val myArray = arrayOf(1, 2, 3)
  20. val myList = listOf("a", "b", "c") // Immutable list
  21. val myMutableList = mutableListOf("a", "b", "c") // Mutable list
  22. val myMap = mapOf("key1" to "value1", "key2" to "value2")
  23. val myMutableMap = mutableMapOf("key1" to "value1")
  24.  
  25. Control Flow
  26. If Expression
  27. val max = if (a > b) a else b
  28.  
  29.  
  30. When Expression (Switch Case)
  31. when (x) {
  32. 1 -> print("x == 1")
  33. 2 -> print("x == 2")
  34. else -> { // Note the block
  35. print("x is neither 1 nor 2")
  36. }
  37. }
  38.  
  39. For Loop
  40. for (item in collection) print(item)
  41. for (i in 1..10) print(i) // Range inclusive
  42.  
  43. While Loop
  44. while (x > 0) {
  45. x--
  46. }
  47.  
  48. do {
  49. val y = retrieveData()
  50. } while (y != null) // Executes at least once
  51.  
  52. Functions
  53. fun sum(a: Int, b: Int): Int {
  54. return a + b
  55. }
  56.  
  57. // Single-expression function
  58. fun multiply(a: Int, b: Int) = a * b
  59.  
  60. // Default parameters
  61. fun greet(name: String, msg: String = "Hello") = println("$msg $name")
  62.  
  63. // Named arguments
  64. greet(name = "World", msg = "Hi")
  65.  
  66. Classes and Objects
  67. class Person(val firstName: String, val lastName: String) {
  68. var age: Int = 0
  69.  
  70. fun greet() {
  71. println("Hello, my name is $firstName $lastName.")
  72. }
  73. }
  74.  
  75. val person = Person("John", "Doe")
  76. person.age = 30
  77. person.greet()
  78.  
  79. // Data class (automatically provides equals(), hashCode(), toString(), copy())
  80. data class User(val name: String, val age: Int)
  81.  
  82. Null Safety
  83. var nullableVar: String? = "This can be null"
  84. nullableVar = null
  85.  
  86. // Safe call operator
  87. println(nullableVar?.length)
  88.  
  89. // Elvis operator
  90. val len = nullableVar?.length ?: -1
  91.  
  92. // Non-null assertion operator (!!)
  93. val nonNullVar = nullableVar!!.length
  94.  
  95. Extension Functions
  96. fun String.addExclamation(): String = this + "!"
  97.  
  98. val excited = "Hello".addExclamation() // "Hello!"
  99.  
  100. Lambdas and Higher-Order Functions
  101. val sumLambda: (Int, Int) -> Int = { x, y -> x + y }
  102. val result = sumLambda(2, 3) // 5
  103.  
  104. // Higher-order function
  105. fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
  106. return operation(a, b)
  107. }
  108.  
  109. val sumResult = operateOnNumbers(2, 3, sumLambda)
  110.  
  111. Coroutines for Asynchronous Programming
  112. // Basic coroutine launch
  113. GlobalScope.launch {
  114. delay(1000L) // Non-blocking delay for 1 second (only useful in coroutines)
  115. println("World!")
  116. }
  117.  
  118. println("Hello,")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement