The document provides examples of different Kotlin programming concepts like arrays, lists, functions, classes, loops, and data types. It shows how to declare and initialize arrays and lists, write functions with different parameters, use if/else, when expressions, and for, while, do-while loops. Class and higher-order functions are also demonstrated.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
35 views3 pages
Kotlin Basics Cheat Sheet
The document provides examples of different Kotlin programming concepts like arrays, lists, functions, classes, loops, and data types. It shows how to declare and initialize arrays and lists, write functions with different parameters, use if/else, when expressions, and for, while, do-while loops. Class and higher-order functions are also demonstrated.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
Comments Array Examples
//single-line comment // once an array is created the size is fixed
/* multiline */ comment val school = arrayOf("shark", "salmon", "‐ minnow", 2) /* blah comment /* nested */ println(java.util.Arrays.toString(school))
blah */ //mixed type array
val numbers = intArrayOf(1,2,3) If - Else If - Else Example //print array for (element in school){ val count = 42 print(element + " ") if (count == 42) { } println ("I have the answer") // concatenate arrays } else if (count > 35 && count < 49) { array3 = array1 + array2 printlin ("The answer is close") // initialize array with code } else { val array = Array (5) { it * 2} println ("The answer eludes me.") } elements cannot be added or removed, except by copying to a new array When Example can loop through elements and index at the same time
cal answerString = when {
Lists count == 42 -> "I have the answer" count > 35 -> "The answer is close." // create list, cannot be changed
else -> "The answer eludes me." val school = listOf("mackerel", "trout")
} // create list, can be changed
val myList = mutableListOf("tuna'", "sal‐ mon") myList.remove("shark") Functions Example Data Types
// create function Type Size(bits) Values
var count:Int = 42 Byte 8 -128 to 127 fun generateAnswerString(): String { Short 16 -32768 to 32767 val answerString = if (count ==42) { Int 32 -231 to 231 -1 "I have the answer." Long 64 -263 to 263 - 1 } else { "The anwer eludes me" Variable Declaration } return answerString val count : Int = 10 value can never change
} var count : Double = 10.01 value can change
val eFloat : Float = 3.14f needs the f suffex Anonymous Function Example val name: String? = null add ? to make a nullable value //Anonymous functions val name : String? = name!! not-null assertion operator val stringLengthFunc: (String) -> Int = {input - throws exception if full > variable interpolation: $count input.length Kotlin variables can't hold null values by default. } // call anonymous function For Loop Examples val stringLength: Int = stringLengthFunc("A‐ // print index and element ndroid") for ((index, element) in school.withIndex()){ println("Item at $index is $element\n") Class Examples } //create class //print range of numbers or letters class Car{ for (i in 'a'..'h') print (i) //properties val wheels = listOf<Wheel>() } For Loop Examples (cont) Void Function Example
> for (i in 5 downTo 1 step 1.5) print(i) // void function
fun printSum(a: Int, b: Int): Unit { While Examples println("sum of $a and $b is ${a + b}") var bubbles = 0 } while (bubbles < 50) { bubbles++ Higher-Order Functions
} // functions that can take functions as arguments
println("$bubbles bubbles in the water/n") fun stringMapper(str:String, mapper: (String) do { -> Int): Int { bubbles-- //Invoke function } while (bubbles > 50) return mapper(str) println("$bubbles bubbles in the water/n") } repeat(2) { //**anonymous function can be called outside the println("A fish is swimming.") parenthesis } stringMapper("Android") { input -> input.length Function with Argument Example }
// call function val answerString = generateAnswerString()
(A Trivia Nerds Guide To The History of The United States - 3) Bill O'Neill - The Great Book of California - The Crazy History of California With Amazing Random Facts & Trivia (2018)