Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It is developed by JetBrains as a pragmatic language for Java interoperability focused on interoperability, safety, tools, and Kotlin extensions. Some key points about Kotlin:
- Announced as an official Android language alongside Java at Google I/O 2017.
- Open source and developed by JetBrains, the company behind IntelliJ IDEA.
- Runs on the JVM and is fully interoperable with Java.
- Safer than Java with features like null safety and data classes.
- Supports coroutines for writing asynchronous and non-blocking code in
6. Google I/O 2017, announced as Android official language along with Java
Developed by Jet Brains (the same company which built IntelliJ IDEA)
Open source
It’s a JVM language and interoperable with Java
Kotlin Overview
More powerful and advance version of Java
9. MyDemo.java
[ Java Code ]
How Java Code Runs?
Compiler MyDemo.class
[ Byte Code ]
JVM
Program
Running
1. Compile Time
[ Handled by JDK that contains Compiler ]
2. Runtime
MyDemo.kt
[ Kotlin Code ]
Compiler MyDemoKt.class
[ Byte Code ]
JVM
Program
Running
1. Compile Time
2. Runtime
How Kotlin Code Runs?
10. String firstName = “Sriyank”;
String firstName = null;
final String country = ”INDIA”;
Java
Getting Started
Kotlin
var firstName: String = “Sriyank”
var firstName: String? = null
val country: String = “INDIA”
11. class Person {
private String name;
public String getName() {
return firstName;
}
public void setName(String name) {
this.name = name;
}
}
Java
Defining Class
Kotlin
class Person {
public var name: String? = null
}
// No explicit Getter and Setter required
// Setter
person.name = “Sriyank”
// Getter
person.name
12. class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
Java
Defining Class
Kotlin
class Person(var name: String?) {
}
// This defines the constructor as well as
the class properties
13. class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
Java
Defining Data Class
Kotlin
data class Person(var name: String?) {
}
// If your class only holds data, use data
keyword
// This generates default getter, setter
and derives equals(), hashCode() and
toString() functions
14. class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
Person p1 = new Person(“Sriyank”)
Person p2 = new Person(“Sriyank”)
Java
Creating Objects
Kotlin
data class Person(var name: String?) {
}
// Create Objects
var p1 = Person(“Sriyank”)
var p2 = Person(“Sriyank”)
15. class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
Person p1 = new Person(“Sriyank”)
Person p2 = new Person(“Sriyank”)
// Compare data
p1.equals(p2) // returns true
// Compare references (objects)
p1 == p2 // returns false
Java
Equality
Kotlin
data class Person(var name: String?) {
}
// Create Objects
var p1 = Person(“Sriyank”)
var p2 = Person(“Sriyank”)
// Compare data
p1 == p2 // returns true
// Compare references (objects)
p1 === p2 // returns false
16. Default Arguments
Kotlin Feature
data class Person(var name: String?, var id = -1) {
}
var p1 = Person(“Sriyank”, 24) // Person(“Sriyank”, 24)
// if you don’t pass any value
var p2 = Person(“Sriyank”) // Person(“Sriyank”, -1)
17. Named Arguments or Named Parameters
Kotlin Feature
data class Person(var name: String?, var id = -1) {
}
var p1 = Person(name = “Sriyank”, id = 24) // Person(“Sriyank”, 24)
var p2 = Person(id = 24, name = “Sriyank”) // Person(“Sriyank”, 24)
// if you don’t pass any value
var p3 = Person(name = “Sriyank”) // Person(“Sriyank”, -1)
18. public int getSum(int a, int b) {
return a + b;
}
// method call
getSum(2, 3) // returns 5
// No return statement? Use ‘void’
Java
Functions
Kotlin
fun getSum(a: Int, b: Int): Int {
return a + b;
}
// method call
getSum(2, 3) // returns 5
// No return statement? Use ‘Unit’
19. class Circle {
static Double PI = 3.14;
}
// Access the static member
Circle.PI // gives you 3.14
Java
Static in Kotlin? à Companion Object
Kotlin
class Circle {
companion object {
var PI: Double = 3.14
}
}
// Access the companion object
Circle.PI // gives you 3.14
20. class Circle {
static Double PI = 3.14;
}
// Access the static member
Circle.PI // gives you 3.14
Java
Object Declaration: Creating Singleton
Kotlin
object Circle {
var PI: Double = 3.14
}
// Access members of object declaration
Circle.PI // gives you 3.14
// Object declaration means the object of
class Circle will be instantiated only ONCE
during application lifecycle
21. class Foo {
private Integer x;
public Foo() {
this.x = 24;
}
}
// Create Object
Foo foo = new Foo() // x initialised to 24
Java
Initializing Properties in a Class
Kotlin
class Foo {
var x: Int
init {
this.x = 24
}
}
// Create Object
var foo = Foo() // x initialised to 24
22. Kotlin Nullability
Kotlin Feature
var name: String = “abc”
name = null // Compilation Error
var name: String? = “abc” // Use a question mark as suffix to data type
name = null // OK
23. String country = null;
// Throws Null Pointer Exception
System.out.print(country.length)
Java
Kotlin Safe Call [?.]: Avoid NullPointerException
Kotlin
var country: String? = null
print(country?.length)
OUTPUT
null
// If country is not null
// country?.length returns length of String
// If country is null,
// Don’t evaluate length, just print null
24. Kotlin Elvis Operator [?:]
Kotlin Feature
CASE 1:
val name = “Sriyank”
val x = name?.length ?: “some_value”
print(x) // OUTPUT: 7 [length of “Sriyank”]
CASE 2:
val name = null
val x = name?.length ?: “some_value”
print(x) // OUTPUT: some_value
35. Default Process
Main UI Thread
Your App
Main UI Thread
Worker or Background Thread
Small
Operations
UI
Interaction
Button
Click
Mathematical
Operations
Small Logical
Operations
Long
Operations
Network
Operation
File
Download
Image
Loading
Database
Queries
A Typical App
37. Default Process
Main UI Thread
Your App
Main UI Thread
Coroutines
Small
Operations
UI
Interaction
Button
Click
Mathematical
Operations
Small Logical
Operations
Perform Operation without effecting main
thread or blocking UI
A Typical App
38. Default Process
Main UI Thread
Your App
Main UI Thread
Global Coroutines
Small
Operations
UI
Interaction
Button
Click
Mathematical
Operations
Small Logical
Operations
Child Coroutine1: File Download
A Typical App
Child Coroutine2: Database Query
39. Kotlin Coroutines
Code snippet from official docs
fun main() {
GlobalScope.launch { // launch a new coroutine in background and continue
// Launch your task here
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main thread continues while coroutine is delayed
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
OUTPUT
Hello,
World
40. // launch a new thread in background
and continue
thread {
Thread.sleep(1000L);
println("World!");
}
Threads
Kotlin Coroutines
Corountines
// launch a new coroutine in background
and continue
GlobalScope.launch {
delay(1000L)
println("World!")
}