This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val area = {l: Int, w: Int -> l * w} | |
println(area(6,4)) | |
val areaOption :(Int, Int) -> Int = {l, w -> l*w} | |
println(areaOption(6,4)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class Car(val make: String, val model: String, val engineSize: String){ | |
open fun accelerate(){ | |
println("vroom vroom") | |
} | |
fun brake(){ | |
println("STOP") | |
} | |
fun reverse(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nullable : String? = null | |
println(nullable?.length) | |
var notNullableExample : String = "something"!! | |
println(notNullableExample) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Immutable hash map/dictionary | |
**/ | |
val contacts = mapOf("Tom" to "0780","Peter" to "4784","Derrick" to "6780" ) | |
println(contacts) | |
println(contacts["Tom"]) | |
println(contacts.get("Peter")) | |
println(contacts.values) | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Mutable lists/arrays use arrayListOf() method | |
Notice it has other methods to add to the list | |
**/ | |
val mutableAlphabetsLists = arrayListOf("c","d","b","a") | |
println(mutableAlphabetsLists) | |
println(mutableAlphabetsLists.size) | |
println(mutableAlphabetsLists.add("x")) | |
println(mutableAlphabetsLists) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Immutable lists/arrays uses the listOf() method | |
together with a few methods for operations on lists/arrays | |
**/ | |
val alphabets = listOf("c","d","b","a") | |
println(alphabets) | |
println(alphabets.sorted()) | |
println(alphabets[2]) | |
println(alphabets.contains("b")) | |
println(alphabets.last()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = 1 | |
when (x){ | |
1 ->println("x is 1") | |
else -> println("x is not equal to 1") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Example with no args | |
fun exampleFunction(){ | |
println("This is an example of a function in Kotlin") | |
} | |
//Example with Args. Remember the data types are declared explicitly | |
fun exampleFunctionWithArgs(someString:String){ | |
println(someString) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val a = 3 | |
val b = 2.4 | |
println(a + b) | |
println(a / b) | |
println(a * b) | |
println(a % b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Escaped strings, means you can use escaped characters | |
val escapedString = "Kotlin is fun because Zombie said \"Kotlin is ...\" " | |
println(escapedString) | |
/** | |
output - Kotlin is fun because Zombie said "Kotlin is ..." | |
Here's a ist of escape characters you could use | |
\t - Inserts tab |
NewerOlder