Skip to content

Instantly share code, notes, and snippets.

@krizzu
Last active July 5, 2018 10:13
Show Gist options
  • Save krizzu/f8ce48a714d824eb383949990f216d01 to your computer and use it in GitHub Desktop.
Save krizzu/f8ce48a714d824eb383949990f216d01 to your computer and use it in GitHub Desktop.
Small Cheatsheet for kotlin
/*
Variables
- Must be initialized with value
- Can be either immutable or mutable
- Can be null, but need to be declared with nullable type (more later)
- Various in Kotlin
- Numbers
- Byte
- Short
- Int
- Long
- Float
- Double
- Boolean
- Char (single character)
- Strings
- Arrays
Immutable references
- uses 'val' keyword
- value cannot be changed once it's assigned.
- similar to 'const' in JS.
- nested references can be modified (like Object's properties can be reassigned in JS)
Mutable references
- uses 'var' keyword
- value can be reassigned
- similar to 'let' in JS
*/
val name: String = "Jerry"
val otherName = "Bob" // Type String is inferred here
var age = 23 // Type inferred to Int
age = 354 // allowed
/*
Flow Control
If/else
- traditional if/else :)
- In Kotlin, it's an expression (replaces ternary operator)
While/Do..while
- traditional while/do..while loops
For loop
- Not a traditional For loop
- Loops through anything that provides an iterator (arrays, ranges, maps)
When expression
- Switch statement on steroids
- is an expression
- can combine branches
- can check values in ranges (later on that)
- can check if values is of particular type
*/
// Example: If/else
val age = 20
val isAllowed = if (age >= 18) true else false
// Example: While
do {
print("Hello")
} while (true)
// Example: For
for (item in array) {
print(item)
}
// Example: When
val number = 50
when(number) {
// Combining branches
10, 20, 30, 40, 50 -> println("Is on of those!")
// check for type
is Int -> println("It's an Int!")
// check if value is in range
in 1..100 -> println("Between 1 and 100!")
}
/*
Functions
- 'fun' keyword used
- must define return value (default Unit - like 'undefined' in JS)
- with single return expression, can omit the body
- parameters must define their type
- arguments can have default values
- arguments can be named
- lambdas & higher order functions supported
*/
// simple function, 'Unit' can be omitted
fun myFunc1(): Unit {}
// omitting body, default arguments
fun myFunc2(title: String = "No_Title"): String = "My title ${title}" // returning string, template string
myFunc2(title = "my superb title") // named argument
// lambda
val myLambda = { myArg: String -> print("My argument: $myArg")}
myLambda("lambda")
/*
OOP
There's a lot to this topic in Kotlin, see https://kotlinlang.org/docs/reference/classes.html
Example below:
MyBaseClass
- has primary constructor with 'name' parameter
- declares public property 'uid'
MyClass
- extends MyBaseClass
- has primary constructor with 'name' and 'age', where 'age' becomes it's public property (because of 'val')
- execute parent primary constructor ("super" in JS) with arguments
- declare a method that returns "random" string
*/
class MyBaseClass(name: String){
val uid = name + "123321"
}
class MyClass (val myName: String, val age: Int) : MyBaseClass(myName) {
fun getUID(): String {
return "$myName-$age-_random123"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment