Introduction To Kotlin
Introduction To Kotlin
Introduction
to 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
fun main() {
println("Hello, world!")
}
Where is “;”???
The basics
Can be a one-liner:
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
var index = 0
while (index < items.size) {
println("item at $index is ${items[index]}")
index++
}
val x = 10
if (x in 1..10) {
println("fits in range")
}
for (x in 1..5) {
print(x)
}
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:
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
The not-null assertion operator (!!) converts any value to a non-null type and throws an NPE
exception if the value is null.
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
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:
If the lambda is the only argument, the parentheses can be omitted entirely (the
documentation calls this feature "trailing lambda as a parameter"):
Go to:
● kotlinlang.org
● kotlinlang.org/docs
● play.kotlinlang.org/byExample
Thanks!