0% 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.

Uploaded by

S A U R A B H
Copyright
© © All Rights Reserved
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% 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.

Uploaded by

S A U R A B H
Copyright
© © All Rights Reserved
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

//sing​le-line comment // once an array is created the size is fixed


/* multiline */ comment val school = arrayO​f("s​har​k", "​sal​mon​", "​‐
min​now​", 2)
/* blah comment
/* nested */ printl​n(j​ava.ut​il.A​rr​ays.to​Str​ing​(sc​hool))

blah */ //mixed type array


val numbers = intArr​ayO​f(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​")
// concat​enate 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​("ma​cke​rel​", "​tro​ut")

} // create list, can be changed


val myList = mutabl​eLi​stO​f("t​una​'", "​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 genera​teA​nsw​erS​tri​ng(): String {
Short 16 -32768 to 32767
val answer​String = 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 Declar​ation
}
return answer​String 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 string​Len​gth​Func: (String) -> Int = {input - throws exception if full
>
variable interp​ola​tion: $count
input.l​ength
Kotlin variables can't hold null values by default.
}
// call anonymous function For Loop Examples
val string​Length: Int = string​Len​gth​Fun​c("A​‐
// print index and element
ndr​oid​")
for ((index, element) in school.wi​thI​nde​x()){
printl​n("Item at $index is $eleme​nt​\n")
Class Examples
}
//create class
//print range of numbers or letters
class Car{
for (i in 'a'..'h') print (i)
//prop​erties
val wheels = listOf​<Wh​eel​>()
}
For Loop Examples (cont) Void Function Example

> for (i in 5 downTo 1 step 1.5) print(i) // void function


fun printS​um(a: Int, b: Int): Unit {
While Examples printl​n("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


printl​n("$​bubbles bubbles in the water/​n") fun string​Map​per​(st​r:S​tring, mapper: (String)
do { -> Int): Int {
bubbles-- //Invoke function
} while (bubbles > 50) return mapper​(str)
printl​n("$​bubbles bubbles in the water/​n") }
repeat(2) { //**an​onymous function can be called outside the
printl​n("A fish is swimmi​ng."​) parent​hesis
} string​Map​per​("An​dro​id") { input ->
input.l​ength
Function with Argument Example }

// call function
val answer​String = genera​teA​nsw​erS​tring()

// function with argument


fun genera​teA​nsw​erS​tring (count​Thr​eshold: Int) :String {
...
}
return answer​String
}

// call function with argument

val answer​String = genera​teA​nsw​erS​tri​ng(42)

You might also like