├── src ├── Basics.kt ├── CONTROL_STRUCTURES.kt └── CLASSES.kt ├── KotlinCheatSheet.pdf ├── out └── production │ └── Kotlin-Cheat-sheet │ ├── META-INF │ └── Kotlin-Cheat-sheet.kotlin_module │ ├── BasicsKt.class │ └── CONTROL_STRUCTURESKt.class ├── .idea ├── vcs.xml ├── misc.xml ├── modules.xml ├── kotlinc.xml ├── libraries │ └── KotlinJavaRuntime.xml └── workspace.xml ├── Kotlin-Cheat-sheet.iml └── README.md /src/Basics.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | println("Hello, World") 3 | 4 | 5 | } 6 | -------------------------------------------------------------------------------- /KotlinCheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohamedebrahim96/Kotlin-Cheat-sheet/HEAD/KotlinCheatSheet.pdf -------------------------------------------------------------------------------- /out/production/Kotlin-Cheat-sheet/META-INF/Kotlin-Cheat-sheet.kotlin_module: -------------------------------------------------------------------------------- 1 |  2 | 3 | " 4 | BasicsKtCONTROL_STRUCTURESKt -------------------------------------------------------------------------------- /out/production/Kotlin-Cheat-sheet/BasicsKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohamedebrahim96/Kotlin-Cheat-sheet/HEAD/out/production/Kotlin-Cheat-sheet/BasicsKt.class -------------------------------------------------------------------------------- /out/production/Kotlin-Cheat-sheet/CONTROL_STRUCTURESKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohamedebrahim96/Kotlin-Cheat-sheet/HEAD/out/production/Kotlin-Cheat-sheet/CONTROL_STRUCTURESKt.class -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 10 | -------------------------------------------------------------------------------- /Kotlin-Cheat-sheet.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/CONTROL_STRUCTURES.kt: -------------------------------------------------------------------------------- 1 | //If as an expression 2 | //For loop 3 | //When expression 4 | //When expression with predicates 5 | fun bigger(a: Int, b: Int) = if (a > b) a else b 6 | val list = listOf("A", "B", "C") 7 | fun main(args: Array) { 8 | println("Hello, World") 9 | for (element in list) { 10 | println(element) 11 | } 12 | } 13 | 14 | fun numberTypeName(x: Number) = when(x) { 15 | 0 -> "Zero" // Equality check 16 | in 1..4 -> "Four or less" // Range check 17 | 5, 6, 7 -> "Five to seven" // Multiple values 18 | is Byte -> "Byte" // Type check 19 | else -> "Some number" 20 | } 21 | fun signAsString(x: Int)= when { 22 | x < 0 -> "Negative" 23 | x == 0 -> "Zero" 24 | else -> "Positive" 25 | } -------------------------------------------------------------------------------- /.idea/libraries/KotlinJavaRuntime.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Kotlin-Cheat-sheet 2 | ============================= 3 | 4 | 5 | You can have the most important elements close at hand — it is the best everyday support for Kotlin developer. Kotlin is a new programming language created by JetBrains and targeting the JVM, Android and the browser. Kotlin is concise, safe, and fully interoperable with existing Java and JavaScript code. Kotlin helps avoid common errors such as NPEs and strives to make programming more pleasant. At the Google I/O conference, Google has announced official support for Kotlin as a language for developing Android applications, meaning that Kotlin development tools are now bundled with Android Studio. This cheat sheet will introduce you to the most important elements of the Kotlin syntax. To learn more, visit the Kotlin web site here. 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/CLASSES.kt: -------------------------------------------------------------------------------- 1 | 2 | 3 | fun main(args: Array) { 4 | //Primary constructor 5 | //Inheritance 6 | //Properties with assessors 7 | //Data classes 8 | //val declares a read-only property, var a mutable one 9 | 10 | 11 | 12 | 13 | val mike = Person("Mike", 23) 14 | //Modifier data adds: 15 | /*1.toString that displays all primary constructor 16 | properties*/ 17 | print(mike.toString()) // Person(name=Mike, age=23) 18 | 19 | /*2.equals that compares all primary constructor 20 | properties*/ 21 | print(mike == Person("Mike", 23)) // True 22 | print(mike == Person("Mike", 21)) // False 23 | /*3.hashCode that is based on all primary 24 | constructor properties*/ 25 | val hash = mike.hashCode() 26 | print(hash == Person("Mike", 23).hashCode()) // True 27 | print(hash == Person("Mike", 21).hashCode()) // False 28 | /*4.component1, component2 etc. that allows 29 | deconstruction*/ 30 | val (name, age) = mike 31 | print("$name $age") // Mike 23 32 | /*5.copy that returns copy of object with concrete 33 | properties changed*/ 34 | val jake = mike.copy(name = "Jake") 35 | 36 | /*partition - splits into pair of lists*/ 37 | val l = arrayOf(1,2,3) 38 | 39 | val (even, odd) = l.partition { it % 2 == 0 } 40 | print(even) // [2, 4] 41 | print(odd) // [1, 3] 42 | /*min / max / minBy / maxBy*/ 43 | l.min() // 1, possible because we can compare Int 44 | l.minBy { -it } // 4 45 | l.max() // 4, possible because we can compare Int 46 | l.maxBy { -it } // 1 47 | //first / firstBy 48 | l.first() // 1 49 | l.first { it % 2 == 0 } // 2 (first even number) 50 | //count - count elements matched by predicate 51 | l.count { it % 2 == 0 } // 2 52 | //sorted / sortedBy - returns sorted collection 53 | listOf(2, 3, 1, 4).sorted() // [1, 2, 3, 4] 54 | l.sortedBy { it % 2 } // [2, 4, 1, 3] 55 | //groupBy - group elements on collection by key 56 | l.groupBy { it % 2 } // Map: {1=[1, 3], 0=[2, 4]} 57 | ///distinct / distinctBy - returns only unique elements 58 | listOf(1, 1, 2, 2).distinct() // [1, 2] 59 | //Mutable vs immutable 60 | //collection processing functions 61 | val list = mutableListOf(3, 4, 2, 1) 62 | val sortedResult = list.sorted() // Returns sorted 63 | println(sortedResult) // [1, 2, 3, 4] 64 | println(list) // [3, 4, 2, 1] 65 | val sortResult = list.sort() // Sorts mutable collection 66 | println(sortResult) // kotlin.Unit 67 | println(list) // [1, 2, 3, 4] 68 | 69 | } 70 | 71 | 72 | class Person(val name: String, var age: Int) 73 | 74 | // name is read-only, age is mutable 75 | /*open class Person(val name: String) { 76 | open fun hello() = "Hello, I am $name" 77 | // Final by default so we need open 78 | }*/ 79 | 80 | /*class PolishPerson(name: String) : Person(name) { 81 | override fun hello() = "Dzień dobry, jestem $name" 82 | }*/ 83 | 84 | /*class Person(var name: String, var surname: String) { 85 | var fullName: String 86 | get() = "$name $surname" 87 | set(value) { 88 | val (first, rest) = value.split(" ", limit = 2) 89 | name = first 90 | surname = rest 91 | } 92 | }*/ 93 | 94 | //data class Person(val name: String, var age: Int) -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 82 | 83 | 84 | 86 | 87 | 94 | 95 | 96 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 |