Show Menu
Cheatography

Kotlin Basics Cheat Sheet (DRAFT) by

A cheat sheet for Kotlin basics for my CSCV 381 Mobile Device Programming class.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Comments

//sing­le-line
comment
/* multiline */
comment
/* blah
/* nested */
blah */
comment

If - Else If - Else Example

val count = 42
if (count == 42) {
  println ("I have the answer")
} else if (count > 35 && count < 49) {
  printlin ("The answer is close")
} else {
  println ("The answer eludes me.")
}

When Example

cal answerString = when {
  count == 42 -> "I have the answer"
  count > 35 -> "The answer is close."
  else -> "The answer eludes me."
}

Array Examples

// once an array is created the size is fixed
val school = arrayOf("shark", "salmon", "minnow", 2)
println(java.util.Arrays.toString(school))

//mixed type array
val numbers = intArrayOf(1,2,3)

//print array
for (element in school){
  print(element + " ")
}

// concatenate arrays
array3 = array1 + array2

// initialize array with code
val array = Array (5) { it * 2}
elements cannot be added or removed, except by copying to a new array
can loop through elements and index at the same time

Lists

// create list, cannot be changed
val school = listOf("mackerel", "trout")

// create list, can be changed
val myList = mutableListOf("tuna'", "salmon") 
myList.remove("shark")

Functions Example

// create function
var count:Int = 42
fun generateAnswerString(): String {
  val answerString = if (count ==42) {
    "I have the answer."
  } else {
    "The anwer eludes me"
  }
  return answerString
}

Anonymous Function Example

//Anonymous functions
val stringLengthFunc: (String) -> Int = {input ->
  input.length
}

// call anonymous function
val stringLength: Int = stringLengthFunc("Android")

Class Examples

//create class
class Car{
  //properties
  val wheels = listOf<Wheel>()
}
 

Data Types

Type
Size(bits)
Values
Byte
8
-128 to 127
Short
16
-32768 to 32767
Int
32
-231 to 231 -1
Long
64
-263 to 263 - 1

Variable Declar­ation

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
val name: String? = null
add ? to make a nullable value
val name : String? = name!!
not-null assertion operator
throws exception if full
variable interp­ola­tion: $count
Kotlin variables can't hold null values by default.

For Loop Examples

// print index and element
for ((index, element) in school.withIndex()){
  println("Item at $index is $element\n")
}

//print range of numbers or letters
for (i in 'a'..'h') print (i)
for (i in 5 downTo 1 step 1.5) print(i)

While Examples

var bubbles = 0
while (bubbles < 50) {
  bubbles++
}
println("$bubbles bubbles in the water/n")

do {
  bubbles--
} while (bubbles > 50)
println("$bubbles bubbles in the water/n")

repeat(2) {
  println("A fish is swimming.")
}

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)

Void Function Example

// void function
fun printSum(a: Int, b: Int): Unit {
  println("sum of $a and $b is ${a + b}")
}

Higher­-Order Functions

// functions that can take functions as arguments
fun stringMapper(str:String, mapper: (String) -> Int): Int {
  //Invoke function
  return mapper(str)
}

//**anonymous function can be called outside the parenthesis
stringMapper("Android") { input ->
  input.length
}