6 prism_languages: [kotlin]
8 [Kotlin](http://kotlinlang.org/) is a statically typed programming language for modern multiplatform applications.
18 var mutableString: String = "Adam"
19 val immutableString: String = "Adam"
20 val inferredString = "Adam"
27 val greeting = "Hello, " + name
28 val greetingTemplate = "Hello, $name"
29 val interpolated = "Hello, ${name.toUpperCase()}"
44 val trueBoolean = true
45 val falseBoolean = false
46 val andCondition = trueBoolean && falseBoolean
47 val orCondition = trueBoolean || falseBoolean
55 val NAME_KEY = "name_key"
59 val key = Person.NAME_KEY
66 ### Nullable properties
69 val cannotBeNull: String = null // Invalid
70 val canBeNull: String? = null // Valid
72 val cannotBeNull: Int = null // Invalid
73 val canBeNull: Int? = null // Valid
79 val name: String? = "Adam"
81 if (name != null && name.length > 0) {
82 print("String length is ${name.length}")
84 print("String is empty.")
91 val nullableStringLength: Int? = nullableString?.length
92 val nullableDepartmentHead: String? = person?.department?.head?.name
98 val nonNullStringLength: Int = nullableString?.length ?: 0
99 val nonNullDepartmentHead: String = person?.department?.head?.name ?: ""
100 val nonNullDepartmentHead: String = person?.department?.head?.name.orEmpty()
105 // Will not throw ClassCastException
106 val nullableCar: Car? = (input as? Car)
116 val numArray = arrayOf(1, 2, 3)
117 val numList = listOf(1, 2, 3)
118 val mutableNumList = mutableListOf(1, 2, 3)
124 val firstItem = numList[0]
125 val firstItem = numList.first()
126 val firstItem = numList.firstOrNull()
132 val faceCards = mutableMapOf("Jack" to 11, "Queen" to 12, "King" to 13)
133 val jackValue = faceCards["Jack"] // 11
140 val immutableList = listOf(1, 2, 3)
141 val mutableList = immutableList.toMutableList()
143 val immutableMap = mapOf("Jack" to 11, "Queen" to 12, "King" to 13)
144 val mutableMap = immutableMap.toMutableMap()
150 for (item in myList) {
158 myList.forEachIndexed { index, item ->
159 print("Item at $index is: $item")
163 ### Filtering & Searching
166 val evenNumbers = numList.filter { it % 2 == 0 }
167 val containsEven = numList.any { it % 2 == 0 }
168 val containsNoEvens = numList.none { it % 2 == 0 }
169 val containsNoEvens = numList.all { it % 2 == 1 }
170 val firstEvenNumber: Int = numList.first { it % 2 == 0 }
171 val firstEvenOrNull: Int? = numList.firstOrNull { it % 2 == 0 }
174 Note: `it` is the [implicit name for a single parameter](https://kotlinlang.org/docs/reference/lambdas.html#it-implicit-name-of-a-single-parameter).
180 ### Parameters & Return Types
187 fun printName(person: Person) {
191 fun getGreeting(person: Person): String {
192 return "Hello, ${person.name}"
195 fun getGreeting(person: Person): String = "Hello, ${person.name}"
196 fun getGreeting(person: Person) = "Hello, ${person.name}"
199 ### Higher Order Functions
202 fun callbackIfTrue(condition: Boolean, callback: () -> Unit) {
208 callbackIfTrue(someBoolean) {
209 print("Condition was true")
213 ### Extension Functions
216 fun Int.timesTwo(): Int {
220 val four = 2.timesTwo()
223 ### Default Parameters
226 fun getGreeting(person: Person, intro: String = "Hello,") {
227 return "$intro ${person.name}"
230 // Returns "Hello, Adam"
231 val hello = getGreeting(Person("Adam"))
233 // Returns "Welcome, Adam"
234 val welcome = getGreeting(Person("Adam"), "Welcome,")
240 class Person(val name: String = "", age: Int = 0)
243 val person = Person()
244 val person = Person("Adam", 100)
245 val person = Person(name = "Adam", age = 100)
246 val person = Person(age = 100)
247 val person = Person(age = 100, name = "Adam")
253 class Fragment(val args: Bundle) {
255 fun newInstance(args: Bundle): Fragment {
256 return Fragment(args)
261 val fragment = Fragment.newInstance(args)
264 * [Companion Objects](https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects)
270 ### Primary Constructor
273 class Person(val name: String, val age: Int)
274 val adam = Person("Adam", 100)
277 ### Secondary Constructors
280 class Person(val name: String) {
281 private var age: Int? = null
283 constructor(name: String, age: Int) : this(name) {
288 // Above can be replaced with default params
289 class Person(val name: String, val age: Int? = null)
292 ### Inheritance & Implementation
296 class Car : Vehicle()
302 class Machine : Runner {
326 for (i in 0..10) { } // 1 - 10
327 for (i in 0 until 10) // 1 - 9
329 for (i in 0 until 10 step 2) // 0, 2, 4, 6, 8
339 SOUTH -> print("South")
340 EAST, WEST -> print("East or West")
341 "N/A" -> print("Unavailable")
342 else -> print("Invalid Direction")
358 Destructuring Declarations
359 --------------------------
365 val person = Person("Adam", 100)
366 val (name, age) = person
368 val pair = Pair(1, 2)
369 val (first, second) = pair
371 val coordinates = arrayOf(1, 2, 3)
372 val (x, y, z) = coordinates
375 ### ComponentN Functions
378 class Person(val name: String, val age: Int) {
379 operator fun component1(): String {
383 operator fun component2(): Int {
393 * [Defining Variables](https://kotlinlang.org/docs/reference/basic-syntax.html#defining-variables) _(kotlinlang.org)_
394 * [Strings Documentation](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) _(kotlinlang.org)_
395 * [String Templates](https://kotlinlang.org/docs/reference/basic-types.html#string-templates) _(kotlinlang.org)_
396 * [Basic Types](https://kotlinlang.org/docs/reference/basic-types.html) _(kotlinlang.org)_
397 * [Companion Objects](https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects) _(kotlinlang.org)_
398 * [Null Safety](https://kotlinlang.org/docs/reference/null-safety.html) _(kotlinlang.org)_
399 * [Collections Overview](https://kotlinlang.org/docs/reference/collections.html) _(kotlinlang.org)_
400 * [Collections Documentation](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index.html) _(kotlinlang.org)_
401 * [Functions Documentation](https://kotlinlang.org/docs/reference/functions.html) _(kotlinlang.org)_
402 * [Classes Documentation](https://kotlinlang.org/docs/reference/classes.html) _(kotlinlang.org)_
403 * [Destructuring Declarations](https://kotlinlang.org/docs/reference/multi-declarations.html) _(kotlinlang.org)_