Last active
May 23, 2024 03:12
Revisions
-
audreyfeldroy revised this gist
Dec 19, 2023 . 1 changed file with 17 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -67,7 +67,7 @@ fun greeting(): String { println(greeting()) ``` Function with parameters and KDoc documentation: ```kotlin fun add(a: Int, b: Int): Int { @@ -82,18 +82,24 @@ fun add(a: Int, b: Int): Int { } ``` ### Single-Expression Functions Return type can be inferred: ```kotlin fun greeting() = "Hello!" ``` ### Default Parameter Values Function with default parameter value: ```kotlin fun greeting(name: String = "Mommy") = "Hello, $name!" ``` ### Named Arguments Function with named arguments: ```kotlin @@ -102,6 +108,14 @@ fun greeting(name: String, greeting: String) = "$greeting, $name!" println(greeting(name = "Mommy", greeting = "Hello")) ``` When calling a function with named arguments, you can change the order of the arguments: ```kotlin println(greeting(greeting = "Hello", name = "Mommy")) ``` ### Varargs Function with varargs: ```kotlin @@ -116,6 +130,8 @@ fun sum(vararg numbers: Int): Int { println(sum(1,2,3)) ``` The default function return type is `Unit`, which is equivalent to `void` in Java. ## Lambdas and Higher-Order Functions Function that takes a lambda as a parameter: -
audreyfeldroy revised this gist
Dec 19, 2023 . 1 changed file with 66 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -82,6 +82,72 @@ fun add(a: Int, b: Int): Int { } ``` Return type can be inferred: ```kotlin fun greeting() = "Hello!" ``` Function with default parameter value: ```kotlin fun greeting(name: String = "Mommy") = "Hello, $name!" ``` Function with named arguments: ```kotlin fun greeting(name: String, greeting: String) = "$greeting, $name!" println(greeting(name = "Mommy", greeting = "Hello")) ``` Function with varargs: ```kotlin fun sum(vararg numbers: Int): Int { var total = 0 for (number in numbers) { total += number } return total } println(sum(1,2,3)) ``` ## Lambdas and Higher-Order Functions Function that takes a lambda as a parameter: ```kotlin fun sum(a: Int, b: Int): Int { return a + b } fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int { return operation(a, b) } val result = calculate(1, 2, ::sum) ``` You can call the same function using a lambda expression instead of a function reference: ```kotlin val result = calculate(1, 2) { a, b -> a + b } ``` Function that returns a lambda: ```kotlin fun operation(): (Int, Int) -> Int { return { a, b -> a + b } } val result = operation()(1, 2) ``` ## Classes ```kotlin @@ -236,27 +302,6 @@ val name = "John Doe" println(name.initials()) ``` ## Generics ```kotlin -
audreyfeldroy revised this gist
Dec 19, 2023 . 1 changed file with 92 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,6 +33,16 @@ Boolean for true/false values: var isPlaying = true ``` ## Data Types ```kotlin val name: String = "Mommy" val age: Int = 30 val height: Double = 5.5 val isPlaying: Boolean = true val pi: Float = 3.14f ``` ## Comments ```kotlin @@ -108,14 +118,96 @@ person.name = "Uma" ## Collections A List is an ordered collection of items. ```kotlin val toys: List<String> = listOf("bear", "doll", "car") val toyCount: Int = toys.size val firstToy: String = toys[0] val lastToy: String = toys.last() val hasCar: Boolean = toys.contains("car") val numbers = mutableListOf(1,2,3) numbers.add(4) ``` A Set is an unordered collection of unique items. ```kotlin val numsSet = setOf(1,2,3) println(numsSet.contains(2)) ``` A Map is a collection of key-value pairs. ```kotlin val toys: Map<String, Int> = mapOf("bear" to 1, "doll" to 2, "car" to 3) val toyCount: Int = toys.size val bearCount: Int = toys["bear"] ?: 0 val hasCar: Boolean = toys.containsKey("car") ``` ## Ranges ```kotlin val numbers = 1..10 val numbers = 1 until 10 val numbers = 10 downTo 1 val numbers = 1..10 step 2 ``` ## Loops ```kotlin for (i in 1..10) { println(i) } for (i in 10 downTo 1 step 2) { println(i) } val toys = listOf("bear", "doll", "car") for (toy in toys) { println(toy) } val toys = mapOf("bear" to 1, "doll" to 2, "car" to 3) for ((toy, count) in toys) { println("$toy: $count") } ``` ## Conditionals ```kotlin val age = 30 if (age < 18) { println("You are a child") } else if (age < 65) { println("You are an adult") } else { println("You are a senior") } ``` ## Exceptions ```kotlin fun divide(a: Int, b: Int): Int { if (b == 0) { throw IllegalArgumentException("Cannot divide by zero") } return a / b } try { divide(1, 0) } catch (e: IllegalArgumentException) { println(e.message) } ``` ## Control Flow ```kotlin -
audreyfeldroy revised this gist
Dec 19, 2023 . 1 changed file with 55 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,6 +33,20 @@ Boolean for true/false values: var isPlaying = true ``` ## Comments ```kotlin // This is a single-line comment /* This is a multi-line comment */ /** * This is a KDoc documentation comment */ ``` ## Functions ```kotlin @@ -43,13 +57,53 @@ fun greeting(): String { println(greeting()) ``` Function with KDoc documentation: ```kotlin fun add(a: Int, b: Int): Int { /** * Adds two numbers together. * * @param a The first number to add. * @param b The second number to add. * @return The sum of the two numbers. */ return a + b } ``` ## Classes ```kotlin class Person(var name: String) val person = Person("John") person.name = "Daddy" ``` ## Inheritance The `open` keyword is required to allow inheritance. By default, classes are final and cannot be inherited from. ```kotlin open class Person(var name: String) class Student(name: String, var grade: Int) : Person(name) val student = Student("Daddy", 5) student.name = "Uma" student.grade = 6 ``` ## Data Classes A data class is a class that only holds data, and has no behavior. ```kotlin data class Person(var name: String) val person = Person("Mommy") person.name = "Uma" ``` ## Collections -
audreyfeldroy revised this gist
Dec 18, 2023 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -150,3 +150,8 @@ class MainActivity : AppCompatActivity() { - [Kotlin Playground](https://play.kotlinlang.org/) - [Kotlin Reference](https://kotlinlang.org/docs/reference/) - [Kotlin Standard Library](https://kotlinlang.org/api/latest/jvm/stdlib/) ## Credits - This cheatsheet was inspired by [Daniel Roy Greenfeld](https://daniel.feldroy.com/)'s [Thoughts on Kotlin](https://daniel.feldroy.com/posts/thoughts-on-kotlin) - GitHub Copilot was used to generate some of the code snippets, which I then modified and checked for correctness -
audreyfeldroy revised this gist
Dec 18, 2023 . 1 changed file with 86 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,12 @@ Double for decimal values: var starValue: Double = 0.1 ``` Boolean for true/false values: ```kotlin var isPlaying = true ``` ## Functions ```kotlin @@ -64,4 +70,83 @@ fun max(a: Int, b: Int): Int { } val result = max(1, 2) ``` ## Null Safety ```kotlin var name: String? = null name = "John" ``` ## Extensions ```kotlin fun String.initials(): String { return this.split(" ").map { it.first() }.joinToString("") } val name = "John Doe" println(name.initials()) ``` ## Lambdas ```kotlin val numbers = listOf(1,2,3) numbers.forEach { println(it) } ``` ## Higher-Order Functions ```kotlin fun sum(a: Int, b: Int): Int { return a + b } fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int { return operation(a, b) } val result = calculate(1, 2, ::sum) ``` ## Generics ```kotlin class Box<T>(t: T) { var value = t } val box = Box(1) ``` ## Coroutines ```kotlin fun main() = runBlocking { launch { delay(1000L) println("World!") } println("Hello,") } ``` ## Android ```kotlin class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } ``` ## Resources - [Kotlin Koans](https://play.kotlinlang.org/koans/overview) - [Kotlin Playground](https://play.kotlinlang.org/) - [Kotlin Reference](https://kotlinlang.org/docs/reference/) - [Kotlin Standard Library](https://kotlinlang.org/api/latest/jvm/stdlib/) -
audreyfeldroy revised this gist
Dec 18, 2023 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,9 +16,15 @@ val pi = 3.14 Increment and decrement operators: ```kotlin var toyCount = 4 toyCount++ toyCount-- ``` Double for decimal values: ```kotlin var starValue: Double = 0.1 ``` ## Functions -
audreyfeldroy revised this gist
Dec 18, 2023 . 1 changed file with 10 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,8 @@ Declare var for a mutable variable: ```kotlin var name = "Mommy" name = "Uma" ``` Declare val for a read-only constant: @@ -13,6 +13,14 @@ Declare val for a read-only constant: val pi = 3.14 ``` Increment and decrement operators: ```kotlin var total = 0 total++ total-- ``` ## Functions ```kotlin -
audreyfeldroy renamed this gist
Dec 18, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
audreyfeldroy renamed this gist
Dec 18, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
audreyfeldroy created this gist
Dec 18, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ ## Variables Declare var for a mutable variable: ```kotlin var name = "John" name = "Jane" ``` Declare val for a read-only constant: ```kotlin val pi = 3.14 ``` ## Functions ```kotlin fun greeting(): String { return "Hello!" } println(greeting()) ``` ## Classes ```kotlin class Person(var name: String) val person = Person("John") person.name = "Jane" ``` ## Collections ```kotlin val numbers = mutableListOf(1,2,3) numbers.add(4) val numsSet = setOf(1,2,3) println(numsSet.contains(2)) ``` ## Control Flow ```kotlin fun max(a: Int, b: Int): Int { return if (a > b) a else b } val result = max(1, 2) ```