SlideShare a Scribd company logo
1 of 26
Kotlin
Introduction
to Kotlin
@kotlin
Why Kotlin?
● Expressiveness/Conciseness
● Safety
● Portability/Compatibility
● Convenience
● High Quality IDE Support
● Community
● Android 👀
● More than a gazillion devices run Java Kotlin
● Lactose free
● Sugar free
● Gluten free
Logo
Name
Kotlin is named after an island in the Gulf of Finland.
Hello, world!
fun main(args: Array<String>) {
println("Hello, world!")
}
fun main() {
println("Hello, world!")
}
fun main() = println("Hello, world!")
Where is “;”???
The basics
fun main(args: Array<String>) {
print("Hello")
println(", world!")
}
● An entry point of a Kotlin application is the main top-level function.
● It accepts a variable number of String arguments that can be omitted.
● print prints its argument to the standard output.
● println prints its arguments and adds a line break.
Variables
val/var myValue: Type = someValue
● var - mutable
● val - immutable
● Type can be inferred in most cases
● Assignment can be deferred
val a: Int = 1 // immediate assignment
var b = 2 // 'Int' type is inferred
b = a // Reassigning to 'var' is okay
val c: Int // Type required when no initializer is provided
c = 3 // Deferred assignment
a = 4 // Error: Val cannot be reassigned
Variables
const val/val myValue: Type = someValue
● const val - compile-time const value
● val - immutable value
● for const val use uppercase for naming
const val NAME = "Kotlin" // can be calculated at compile-time
val nameLowered = NAME.lowercase() // cannot be calculated at compile-time
Functions
fun sum(a: Int, b: Int): Int {
return a + b
}
fun mul(a: Int, b: Int) = a * b
fun printMul(a: Int, b: Int): Unit {
println(mul(a, b))
}
fun printMul1(a: Int = 1, b: Int) {
println(mul(a, b))
}
fun printMul2(a: Int, b: Int = 1) = println(mul(a, b))
Single expression function.
Unit means that the function does not
return anything meaningful.
It can be omitted.
Arguments can have default values.
If expression
fun maxOf(a: Int, b: Int) =
if (a > b) {
a
} else {
b
}
is the same as
fun maxOf(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
if can be an expression (it can return).
Can be a one-liner:
fun maxOf(a: Int, b: Int) = if (a > b) a else b
When expression
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> {
print("x is neither 1 nor 2")
}
}
when returns, the same way that if does.
when {
x < 0 -> print("x < 0")
x > 0 -> print("x > 0")
else -> {
print("x == 0")
}
}
The condition can be inside of the branches.
When statement
fun serveTeaTo(customer: Customer) {
val teaSack = takeRandomTeaSack()
when (teaSack) {
is OolongSack -> error("We don't serve Chinese tea like $teaSack!")
in trialTeaSacks, teaSackBoughtLastNight ->
error("Are you insane?! We cannot serve uncertified tea!")
}
teaPackage.brew().serveTo(customer)
}
when can accept several options in one branch. else branch can be omitted if when block is used as a
statement.
&& vs and
if (a && b) { ... } VS if (a and b) { ... }
Unlike the && operator, this function does not perform short-circuit evaluation.
The same behavior with OR:
if (a || b) { ... } VS if (a or b) { ... }
Loops
val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
println(item)
}
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
for ((index, item) in items.withIndex()) {
println("item at $index is $item")
}
Loops
val items = listOf("apple", "banana", "kiwifruit")
var index = 0
while (index < items.size) {
println("item at $index is ${items[index]}")
index++
}
var toComplete: Boolean
do {
...
toComplete = ...
} while(toComplete)
The condition variable can be initialized inside to the do…while loop.
Loops
There are break and continue labels for loops:
myLabel@ for (item in items) {
for (anotherItem in otherItems) {
if (...) break@myLabel
else continue@myLabel
}
}
Ranges
val x = 10
if (x in 1..10) {
println("fits in range")
}
for (x in 1..5) {
print(x)
}
for (x in 9 downTo 0 step 3) {
print(x)
}
downTo and step are extension functions, not keywords.
'..' is actually T.rangeTo(that: T)
Null safety
val notNullText: String = "Definitely not null"
val nullableText1: String? = "Might be null"
val nullableText2: String? = null
fun funny(text: String?) {
if (text != null)
println(text)
else
println("Nothing to print :(")
}
fun funnier(text: String?) {
val toPrint = text ?: "Nothing to print :("
println(toPrint)
}
Elvis operator ?:
If the expression to the left of ?: is not null, the Elvis operator
returns it; otherwise, it returns the expression to the right.
Note that the expression on the right-hand side is evaluated only if
the left-hand side is null.
fun loadInfoById(id: String): String? {
val item = findItem(id) ?: return null
return item.loadInfo() ?: throw
Exception("...")
}
Safe Calls
someThing?.otherThing does not throw an NPE if someThing is null.
Safe calls are useful in chains. For example, an employee may be assigned to a department (or not).
That department may in turn have another employee as a department head, who may or may not
have a name, which we want to print:
fun printDepartmentHead(employee: Employee) {
println(employee.department?.head?.name)
}
To print only for non-null values, you can use the safe call operator together with let:
employee.department?.head?.name?.let { println(it) }
Unsafe Calls
Please, avoid using unsafe calls!
The not-null assertion operator (!!) converts any value to a non-null type and throws an NPE
exception if the value is null.
fun printDepartmentHead(employee: Employee) {
println(employee.department!!.head!!.name!!)
}
TODO
Always throws a NotImplementedError at run-time if called, stating that operation is not
implemented.
// Throws an error at run-time if calls this function, but compiles
fun findItemOrNull(id: String): Item? = TODO("Find item $id")
// Does not compile at all
fun findItemOrNull(id: String): Item? = { }
String templates and the string builder
val i = 10
val s = "Kotlin"
println("i = $i")
println("Length of $s is ${s.length}")
val sb = StringBuilder()
sb.append("Hello")
sb.append(", world!")
println(sb.toString())
Lambda expressions
val sum: (Int, Int) -> Int = { x: Int, y: Int -> x + y }
val mul = { x: Int, y: Int -> x * y }
According to Kotlin convention, if the last parameter of a function is a function, then a lambda
expression passed as the corresponding argument can be placed outside the parentheses:
val badProduct = items.fold(1, { acc, e -> acc * e })
val goodProduct = items.fold(1) { acc, e -> acc * e }
If the lambda is the only argument, the parentheses can be omitted entirely (the documentation calls
this feature "trailing lambda as a parameter"):
run({ println("Not Cool") })
run { println("Very Cool") }
When in doubt
Go to:
● kotlinlang.org
● kotlinlang.org/docs
● play.kotlinlang.org/byExample
Thanks!
@kotlin

More Related Content

Similar to Introduction to Kotlin.pptx

Introduction to ES2015
Introduction to ES2015Introduction to ES2015
Introduction to ES2015kiranabburi
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
KotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxKotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxIan Robertson
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05cKaz Yoshikawa
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014hwilming
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Tomohiro Kumagai
 
pure-functional-programming.pdf
pure-functional-programming.pdfpure-functional-programming.pdf
pure-functional-programming.pdfPuneetChaturvedi23
 
3 kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not3  kotlin vs. java- what kotlin has that java does not
3 kotlin vs. java- what kotlin has that java does notSergey Bandysik
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better JavaGarth Gilmour
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to SwiftGiordano Scalzo
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
JavaScript 1 for high school
JavaScript 1 for high schoolJavaScript 1 for high school
JavaScript 1 for high schooljekkilekki
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on AndroidDeepanshu Madan
 

Similar to Introduction to Kotlin.pptx (20)

Introduction to ES2015
Introduction to ES2015Introduction to ES2015
Introduction to ES2015
 
Kotlin
KotlinKotlin
Kotlin
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
KotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxKotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptx
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)
 
pure-functional-programming.pdf
pure-functional-programming.pdfpure-functional-programming.pdf
pure-functional-programming.pdf
 
3 kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not3  kotlin vs. java- what kotlin has that java does not
3 kotlin vs. java- what kotlin has that java does not
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better Java
 
Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage Go
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
JavaScript 1 for high school
JavaScript 1 for high schoolJavaScript 1 for high school
JavaScript 1 for high school
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on Android
 

Recently uploaded

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 

Recently uploaded (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 

Introduction to Kotlin.pptx

  • 2. Why Kotlin? ● Expressiveness/Conciseness ● Safety ● Portability/Compatibility ● Convenience ● High Quality IDE Support ● Community ● Android 👀 ● More than a gazillion devices run Java Kotlin ● Lactose free ● Sugar free ● Gluten free
  • 4. Name Kotlin is named after an island in the Gulf of Finland.
  • 5. Hello, world! fun main(args: Array<String>) { println("Hello, world!") } fun main() { println("Hello, world!") } fun main() = println("Hello, world!") Where is “;”???
  • 6. The basics fun main(args: Array<String>) { print("Hello") println(", world!") } ● An entry point of a Kotlin application is the main top-level function. ● It accepts a variable number of String arguments that can be omitted. ● print prints its argument to the standard output. ● println prints its arguments and adds a line break.
  • 7. Variables val/var myValue: Type = someValue ● var - mutable ● val - immutable ● Type can be inferred in most cases ● Assignment can be deferred val a: Int = 1 // immediate assignment var b = 2 // 'Int' type is inferred b = a // Reassigning to 'var' is okay val c: Int // Type required when no initializer is provided c = 3 // Deferred assignment a = 4 // Error: Val cannot be reassigned
  • 8. Variables const val/val myValue: Type = someValue ● const val - compile-time const value ● val - immutable value ● for const val use uppercase for naming const val NAME = "Kotlin" // can be calculated at compile-time val nameLowered = NAME.lowercase() // cannot be calculated at compile-time
  • 9. Functions fun sum(a: Int, b: Int): Int { return a + b } fun mul(a: Int, b: Int) = a * b fun printMul(a: Int, b: Int): Unit { println(mul(a, b)) } fun printMul1(a: Int = 1, b: Int) { println(mul(a, b)) } fun printMul2(a: Int, b: Int = 1) = println(mul(a, b)) Single expression function. Unit means that the function does not return anything meaningful. It can be omitted. Arguments can have default values.
  • 10. If expression fun maxOf(a: Int, b: Int) = if (a > b) { a } else { b } is the same as fun maxOf(a: Int, b: Int): Int { if (a > b) { return a } else { return b } } if can be an expression (it can return). Can be a one-liner: fun maxOf(a: Int, b: Int) = if (a > b) a else b
  • 11. When expression when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { print("x is neither 1 nor 2") } } when returns, the same way that if does. when { x < 0 -> print("x < 0") x > 0 -> print("x > 0") else -> { print("x == 0") } } The condition can be inside of the branches.
  • 12. When statement fun serveTeaTo(customer: Customer) { val teaSack = takeRandomTeaSack() when (teaSack) { is OolongSack -> error("We don't serve Chinese tea like $teaSack!") in trialTeaSacks, teaSackBoughtLastNight -> error("Are you insane?! We cannot serve uncertified tea!") } teaPackage.brew().serveTo(customer) } when can accept several options in one branch. else branch can be omitted if when block is used as a statement.
  • 13. && vs and if (a && b) { ... } VS if (a and b) { ... } Unlike the && operator, this function does not perform short-circuit evaluation. The same behavior with OR: if (a || b) { ... } VS if (a or b) { ... }
  • 14. Loops val items = listOf("apple", "banana", "kiwifruit") for (item in items) { println(item) } for (index in items.indices) { println("item at $index is ${items[index]}") } for ((index, item) in items.withIndex()) { println("item at $index is $item") }
  • 15. Loops val items = listOf("apple", "banana", "kiwifruit") var index = 0 while (index < items.size) { println("item at $index is ${items[index]}") index++ } var toComplete: Boolean do { ... toComplete = ... } while(toComplete) The condition variable can be initialized inside to the do…while loop.
  • 16. Loops There are break and continue labels for loops: myLabel@ for (item in items) { for (anotherItem in otherItems) { if (...) break@myLabel else continue@myLabel } }
  • 17. Ranges val x = 10 if (x in 1..10) { println("fits in range") } for (x in 1..5) { print(x) } for (x in 9 downTo 0 step 3) { print(x) } downTo and step are extension functions, not keywords. '..' is actually T.rangeTo(that: T)
  • 18. Null safety val notNullText: String = "Definitely not null" val nullableText1: String? = "Might be null" val nullableText2: String? = null fun funny(text: String?) { if (text != null) println(text) else println("Nothing to print :(") } fun funnier(text: String?) { val toPrint = text ?: "Nothing to print :(" println(toPrint) }
  • 19. Elvis operator ?: If the expression to the left of ?: is not null, the Elvis operator returns it; otherwise, it returns the expression to the right. Note that the expression on the right-hand side is evaluated only if the left-hand side is null. fun loadInfoById(id: String): String? { val item = findItem(id) ?: return null return item.loadInfo() ?: throw Exception("...") }
  • 20. Safe Calls someThing?.otherThing does not throw an NPE if someThing is null. Safe calls are useful in chains. For example, an employee may be assigned to a department (or not). That department may in turn have another employee as a department head, who may or may not have a name, which we want to print: fun printDepartmentHead(employee: Employee) { println(employee.department?.head?.name) } To print only for non-null values, you can use the safe call operator together with let: employee.department?.head?.name?.let { println(it) }
  • 21. Unsafe Calls Please, avoid using unsafe calls! The not-null assertion operator (!!) converts any value to a non-null type and throws an NPE exception if the value is null. fun printDepartmentHead(employee: Employee) { println(employee.department!!.head!!.name!!) }
  • 22. TODO Always throws a NotImplementedError at run-time if called, stating that operation is not implemented. // Throws an error at run-time if calls this function, but compiles fun findItemOrNull(id: String): Item? = TODO("Find item $id") // Does not compile at all fun findItemOrNull(id: String): Item? = { }
  • 23. String templates and the string builder val i = 10 val s = "Kotlin" println("i = $i") println("Length of $s is ${s.length}") val sb = StringBuilder() sb.append("Hello") sb.append(", world!") println(sb.toString())
  • 24. Lambda expressions val sum: (Int, Int) -> Int = { x: Int, y: Int -> x + y } val mul = { x: Int, y: Int -> x * y } According to Kotlin convention, if the last parameter of a function is a function, then a lambda expression passed as the corresponding argument can be placed outside the parentheses: val badProduct = items.fold(1, { acc, e -> acc * e }) val goodProduct = items.fold(1) { acc, e -> acc * e } If the lambda is the only argument, the parentheses can be omitted entirely (the documentation calls this feature "trailing lambda as a parameter"): run({ println("Not Cool") }) run { println("Very Cool") }
  • 25. When in doubt Go to: ● kotlinlang.org ● kotlinlang.org/docs ● play.kotlinlang.org/byExample

Editor's Notes

  1. See Kotlin island on Google Maps :)
  2. Semicolons are not mandatory nor prohibited. An expression can end with a semicolon, but it does not need to if it is separated from the next expression with a new line symbol.
  3. See the docs for more on the program entry point. The next several slides cover the “Basic syntax” section of Kotlin docs.
  4. See the docs for more on variables. Deferred variables SHOULD BE ASSIGNED before being used. However, the full description of how to use deferred assignment is rather complex, so it’s recommended that you don’t use it unless you need to. Also, don’t confuse mutable variables and mutable values. A mutable variable lets you assign another value to it. A mutable value lets you mutate the variable’s value preserving the variable’s reference to the value.
  5. See the docs for more on Properties.
  6. See the docs for more on functions: here and here.
  7. See the docs for more on conditional expressions and if.
  8. See the docs for more on when. when evaluates the first suitable branch (if one exists). But beware of non-pure branch conditions, which when computes lazily. To be more precise, when consecutively evaluates branch conditions and, as soon as it finds the first branch condition that returns true, the when block starts evaluating the branch body. Thus, the when block evaluates all conditions up to and including the condition of the branch that will be evaluated (which also is the first condition that has a value of true), but no further conditions. It works this way because each when block is equivalent to consecutive if-else blocks. For example, when (15 as Number) { 1 -> doFirst() in listOf(2, 3, 4, 5, 6) -> doSecond() is Double -> doThird() else -> doFourth() } is equivalent to if (x == 1) { doFirst() } else if (x in listOf(2, 3, 4, 5, 6)) { doSecond() } else if (x is Double) { doThird() } else { doFourth() } In addition, when { … } is equivalent to when (true) { … } When any value is provided, possible checks are equality checks (as 1 -> { … }), type checks (as is Int -> { … }) and inclusion checks (as in listOf(1, 2, 3, 5, 8, 13) -> { … }). Furthermore, when can be used as an expression instead of a statement: val discountPercentage = when (cost) { in 0 until 300 -> 0 in 300 until 1500 -> 5 else -> 15 }
  9. See the docs for more on when.
  10. The pros and cons of the lazy and eager versions of the “and” and “or” operators: The && and || operators cannot be overridden (they are built-in operations). If you want to apply an “and”/”or” operation to something other than Boolean, then you have to use and/or (they are infix functions and thus overridable). Operator overriding will be covered in the OOP lecture. There are cases when both the left-hand and right-hand sides of the &&/|| operator have side effects (not pure, i.e. they do something other than computing the Boolean value or changing some state) and you need to compute the right-hand value regardless of the left-hand value. Then you should use and/or. For example, result = result and someComputation(). In that case, someComputation() will be executed regardless of the result variable’s value.
  11. See the docs for more on loops: here and here. In the third case, a destructuring declaration is used in the (index, item) declaration. It will be discussed in more detail in the OOP introduction lecture. You can also read about it in this doc article.
  12. See the docs for more on while loops: here and here.
  13. See the docs for more on returns.
  14. See the docs for more on ranges.
  15. See the docs for more on null checks and null safety. The text in the if (text != null) { … } block here is smart-cast to String, i.e. the compiler does understand that text is a String because the if expression checks that text cannot be null. See the docs for more on null checks. Smart casts are highlighted in some IDEs (for example, in IntelliJ IDEA, unless the option is turned off in settings). Smart casts will be discussed in more detail in the FP and JVM & Kotlin compiler lectures. You can also refer to the docs for more on smart casts.
  16. See the docs for more on the Elvis operator. x ?: y is equivalent to if (x != null) x else y. This fully describes the Elvis operator’s behavior.
  17. See the docs for more on null safety. More information about scope functions (let, apply, and others) can be found here. It makes more sense to read this documentation after the OOP introduction lecture.
  18. See the docs for more on the !! operator.
  19. See the docs for more on TODO here and here. Also see other similar functions: error, require, requireNotNull, check, checkNotNull, and assert. TODO is usually used to prototype some logic but defer its implementation, because it’s hard to implement all the logic at once. It’s easier to implement it incrementally by assigning all necessary entities (classes, interfaces, and their methods and values) with TODOs and then replacing the TODOs with actual implementations step by step, from bottom to top. Because of how TODO is implemented, it will allow the stub code to compile. Also, all TODO usages are added to the TODO/FIXME/etc. list in IntelliJ IDEA, so you can find everything you need to implement in the blink of an eye and be sure nothing is missed. Also, error, require, requireNotNull, check, and checkNotNull are very useful shortcuts for throwing exceptions (on certain predicates). error("<error description>”) is a shortcut for throw IllegalStateException("<error description>") which is used when something does not go as planned and you need to throw an exception about it. check(predicate) { "<error description>” } is a shortcut for if (!predicate) throw IllegalStateException("<error description>") which is used in the same way as error but checks a predicate for you. require is a shortcut for if (!predicate) throw IllegalArgumentException("<error description>") which is used in the same way as check but when function input is incorrect (in wrong form) and you need to throw an exception related to it.
  20. See the docs for more on string templates here and here. The last example is good to know and understand, but you’re better off using buildString in real life. For instance, the last example can be idiomatically written as follows: val string = buildString { append("Hello") append(", world!") } println(string) What’s going on here will be discussed in the OOP introduction lecture.
  21. See the docs for more on trailing lambdas here and here.
  22. In the Kotlin docs, the language (syntax) part of Kotlin is covered in the “Basics” and “Concepts” sections. Standard library references are found in the “API reference” section. Formal language reference can be found in the “Language reference” section.