2. Overview
● Kotlin is a new programming language from
JetBrains
● First appeared in 2011, stable release 1.0 in Feb,
2016
● “Statically typed programming language”
● Procedural as well as Functional Programming
both
● Google announced official support for Kotlin on
Android at Google I/O, 2017
3. Benefits
● Kotlin compiles to JVM bytecode or JavaScript
● Kotlin programs can use all existing Java
Frameworks and Libraries
● Kotlin can be learned easily
● Automatic conversion of Java to Kotlin with
Android Studio
● Kotlin’s null-safety is great
6. Inter-operable With Java
Java code can be used inside Kotlin code and vice-versa.
class Person {
String name = “Person Nath”
public String getNameWithoutSpace() {
return name.removeSpaces();
}
}
8. NPE Safety
var name_1: String = "abc"
name_1 = null // try to assign null, won’t compile.
// To allow nulls, we declare a variable as nullable string
var name_2: String? = "abc"
name_2 = null // no compilation error.
val len = name_1.length // there will be no NPE
val len = name_2.length // error: variable 'name_2' can be null
9. NPE Safety
1. Checking for null in conditions
val len = if (name_2 != null) name_2.length else -1
2. Safe Calls (with ?.)
name_2?.length // name_2.length if name_2 is not null, and null otherwise
3. The !! Operator (for NPE Lovers) - not-null assertion operator (!!)
val l = b!!.length // (!!) converts any value to a non-null type
// and throws an exception if the value is null
10. Coroutines (experimental)
● Threads can easily eat up almost a megabyte of memory.
In this context, we can call coroutines (as per the
documentation) as "lightweight" threads.
● A coroutine is sitting on an actual thread pool, which is
used for background execution.
● It only uses the resources when it needs it.
11. launch{}
val job = launch {
val userString = fetchUserString("1")
val user = deserializeUser(userString)
log(user.name)
}
The wrapped code is dispatched to a background thread, and
the function itself returns a Job instance, which can be used in
other coroutines to control the execution.
12. async{}
val user = async {
val userString = fetchUserString("1")
val user = deserializeUser(userString)
user
}.await()
The async function returns a Defered<T> instance, and calling
await() on it you can get the actual result of the computation.
21. ‘When’ Expression
Java
switch(x) {
case 1: log.info("x == 1"); break;
case 2: log.info("x == 2"); break;
default: log.info("x is neither 1 nor
2");
}
Kotlin
when (x) {
in 0,1 -> print("x is too low")
in 2..10 -> print("x is in range")
!in 10..20 -> print("x outside range")
parseInt(s) -> print("s encodes x")
is Program -> print("It's a program!")
else -> print("None of the above")
}
25. ● JavaScript target, allowing you to compile Kotlin code to JS and to run it in
your browser
● Reuse code between the JVM and JavaScript
● Business logic of your application once, and reuse it across all tiers of your
application – the backend, the browser frontend and the Android mobile app