Show Menu
Cheatography

Kotlin Cheat Sheet (DRAFT) by

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

General Tips

Types are capita­lized
You do NOT need ; at the end of lines.
Java uses "­swi­tch­", Kotlin uses "­whe­n".
Kotlin reads like English. When you see this:
Think this:
Example:
:
"Is type"
var age : Int = 10
->
"­ret­urn­s"
when (x) is 12 -> "It's a dozen"
Packages and Imports are best understood by reading the info here: Packages and Imports

Variables

var is a Variable that can be changed.
val is a Value that never changes, like your name.
val a: Int = 1
a is initia­lized and it's type is specified.
var b = 2
b is initia­lized and it's type is inferred. ( You do not need to specify type when you declare unless the compiler guesses wrong. Normally the compiler is very good at inferring the type.)
var c : Int
c isn't initia­lized so it's type must be specified. This can lead to nulls which are evil JuJu in Kotlin. (Bad Dev, no coffee for you.)
var d : String? = "­Nul­lab­le"
The '?' means this is specified with a nullable string. (You are forcing the compiler to allow a null value... why?? Don't do this unless you have a compelling reason.)
var e : String = "­not­Nul­lab­le"
This is specified with a non-nu­llable string.
Avoid nulls if at all possible. This is because in Kotlin a great deal of effort has gone into trying to eliminate null pointer exceptions and the null safety that is one of Kotlin's greatest assets is undermined if you intent­ionally let things be nullable by using the '?'

Number Types

The usual:
Type
Bit Width
 
Double
64
 
Float
32
 
Long
64
 
Int
32
 
Short
16
 
Byte
8
Floating Point Notation:
 
Longs are tagged with 'L'
1000L
 
Floats are tagged with 'f' or 'F'
1000f
1000F
Literal Constants:
Decimal
100
125.5
 
Hex
0xFF342
 
Binary
0b101101
 
Octal
Not Supported
Range
range = 1..10
Contains all Integers from 1 to 10.
Since Kotlin 1.1 you can make numbers more readable with unders­cores:
val oneMillion = 1_000_000
val credit­Car­dNumber = 1234_5­678­_90­12_­3456L
val social­Sec­uri­tyN­umber = 999_99­_9999L
val hexBytes = 0xFF_E­C_DE_5E
val bytes = 0b1101­001­0_0­110­100­1_1­001­010­0_1­0010010

Visibility Modifiers:

Public can be seen by anyone, "who sees the declaring class".
Internal can only be accessed from within the same module / package. (This is great for library authors and should be the standard for Android apps that have all the code in a single package.)
Protected is visible inside the class AND subcla­sses.
Private is visible inside the class only.
Note: In an Android app that has all of it's code in one package there is never any reason to have anything be Public. Instead, make all of your declar­ations Internal, Protected or Private.

Comments

// Line Comment
/* Block
Comment */
/** KDoc
Comment */

Null Safety

var a : String = null
You know better by now. This won't fly.
var a : String? = null
The '?' says you want to allow for the possib­ility of a null. It's not recomm­ended unless you have to make an allowance for a null, such as intero­per­ability with Java code that is already written to allow for nulls.
Some things can cause issues, such as checking the length of a declared but uninit­ialized array and assigning it to a non-nu­llable Int:
var a : Array

var b : Int = a.length

Is a recipe for a crash.
Checking for a null first prevents us from shooting ourselves in the foot. We have a few ways to do it.
Using an If:
if(a != null) a.length else -1


Single line if's are easier than the classic if which would read:
if(a != null){
     return a.length
     } else {
     return -1
}
 
Safe Calls. "­Verify that this isn't a null before doing anything."
a?.length

In context, this declar­ation says that length­OfArray is not allowed to be null but then the code uses a Safe Call (?) to check the length of an array that wasn't initialized:
var a : Array
lengthOfArray : Int = a?.length
Trying to assign a nullable to a non-nu­llable causes your computer to leak magic smoke.
var a =5
var b? : Int = 10
a = b

//No good. Can't do that even though neither one is null because 'a' is non-nu­llable but 'b' is nullable. This is because 'b' could potent­ially be null and 'a' cannot, so this won't fly even though a value was assigned. *Think of
Int
and
Int?
as two different types and you need to cast an
Int
to an
Int?
, because you do.
And we all know that assigning a non-nu­llable value to a nullable is fine.
var a : Int?
var b = 5
a = b

// We do this all the time

Flow Control

If, Else If, Else

if (a > b) {
     return a
     } else if ( b < c ) {
     return b
     } else {
     magicSmokeLeakedOutOfLaptop()
}
When (It's called Switch in Java and C#)

when (x) {

     "Hot Dog" -> print("Mustard")

     true -> lie = false

     42 -> "­Secret is that none of the selections are in {} code blocks."

     else -> { tip = "But notice that the else statement
                    IS in a code block"
}

}
Nested For Loops

fun forRange() {

 ­ ­ ­ ­ // Run 3 Outer Loops
 ­ ­ ­ ­ for (item in 1..3) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­pri­ntl­n("Outer loop #$item \n\nThe inner loop is:")

 ­ ­ ­ ­ ­ ­ ­ ­ ­ for (item in 1..10) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ // in the inner loop, print the even numbers in the range 1..10
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ if (item % 2 == 0) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­pri­ntl­n("$item is even.")
          }
          }
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­pri­nt(­"­Inner loop finished.\n\n")
     }
}
For Each

fun forArray() {
 ­ ­ ­ ­ var testArray: Array<­Str­ing> = arrayO­f("F­irs­t", "­Sec­ond­", "­Thi­rd", "­Fou­rth­", "Fifth")
 ­ ­ ­ ­ // For each thing in the array, print each one.
 ­ ­ ­ ­ for (each in testArray) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ // Yes, it's this simple
          println(each)
     }
}
Sample While

fun sample­While() {
 ­ ­ ­ ­ var count = 1

 ­ ­ ­ ­ ­pri­ntl­n("­\nSample While:")

 ­ ­ ­ ­ ­while (count <= 5) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­pri­ntl­n("Count is: $count")

          count++
     }

}


Sample Out of Range While
// A while loop might never execute if it's test fails
fun sample­Out­OfR­ang­eWh­ile() {
 ­ ­ ­ ­ var countI­sTo­oLarge = 10

 ­ ­ ­ ­ ­pri­ntl­n("­\nSample Out of Range While (Won't Print)")

 ­ ­ ­ ­ // This will never execute at all because the condition
 ­ ­ ­ ­ // is tested before the code is executed.
 ­ ­ ­ ­ ­while (count­IsT­ooLarge <= 5)
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­pri­ntl­n("Count too large count is: $countIsTooLarge")
     countIsTooLarge++

}


Sample Do While
fun sample­DoW­hile() {
 ­ ­ ­ ­ var count = 1

 ­ ­ ­ ­ ­pri­ntl­n("­\nSample Do While")

 ­ ­ ­ ­ do {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­pri­ntl­n("Do While count is: $count")
          count++
 ­ ­ ­ ­ } while (count <= 5)
}


Sample Out of Range Do While
// A Do While will always exectue at least once because
// it's test isn't performed until after the block executes.
fun sample­Out­OfR­ang­eDo­While() {
 ­ ­ ­ ­ var count = 1

 ­ ­ ­ ­ ­pri­ntl­n("­\nSample Out of Range Do While­\n(P­rints once before condition is checked)")

 ­ ­ ­ ­ do {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­pri­ntl­n("Do While count is: $count")
          count++
 ­ ­ ­ ­ } while (count >= 5)
}

Continue and Break

//Cont­inue:
fun main(args: Array<­Str­ing­>) {

 ­ ­ ­ ­ ­pri­ntl­n("Using a not equal test to skip #4:\n")
     useNotEqualToSkip()
 ­ ­ ­ ­ ­pri­ntl­n("­\nUsing continue to skip when count = 4:\n")
     useContinueToSkip()
 ­ ­ ­ ­ ­pri­ntl­n("\nYou should see the exact same output.")

 ­ ­ ­ ­ ­pri­ntl­n("\nNow let's use break to skip out after #3:\n")
     useBreakToGetOut()

}

fun useNot­Equ­alT­oSkip() {
 ­ ­ ­ ­ for (count in 1..5) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ if (count != 4) {
               println(count)
     }
     }
}

fun useCon­tin­ueT­oSkip() {
 ­ ­ ­ ­ for (count in 1..5) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ if (count == 4) {
               continue
          }
          println(count)
     }
}


//Break
// This code will break you out of the loop after #3
fun useBre­akT­oGe­tOut() {
 ­ ­ ­ ­ for (count in 1..5) {
 ­ ­ ­ ­ ­ ­ ­ ­ ­ if (count == 4) {
               break
          }
          println(count)
     }
}
In short:
continue: Go back to the beginning of the block (the for loop)
break: Break out of the block completely and move on. (Leave the loop.)

For continue there are two examples, one uses a != test to skip over 4 and the other uses an if to check if the count is = 4 and then utilizes a continue to skip the rest of the code block and just go back to the beginning of the loop.

The break is simple enough. When the break is called you exit the for loop entirely.