SlideShare une entreprise Scribd logo
1  sur  48
Télécharger pour lire hors ligne
Kotlin CoroutinesKotlin Fundamentals
Kotlin/Everywhere
Create Basic Kotlin Project using
IntelliJ IDEA
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
Just Like Facebook Chat Bubble
More preferred than ever…
Just Like Facebook Chat Bubble
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?
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”
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
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
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
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”)
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
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)
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)
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’
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
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
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
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
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
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
Kotlin String Templates
Kotlin Feature
val firstName = “Sriyank”
print(“{$firstName}”) // OUTPUT: Sriyank
val lastName = “Siddhartha”
print(“$lastName”) // OUTPUT: Siddhartha
print(“${firstName} ${lastName}”) // or print(“$firstName $lastName”)
// OUTPUT: Sriyank Siddhartha
Extension Function
Kotlin Collections
Scope Functions: let, apply, with,
run, also
Lets Do CodeLab
Kotlin Basics Codelab:
http://bit.ly/kotlin-from-java
Coroutines
Create Basic Kotlin Project with
Coroutines dependencies using IntelliJ
IDEA
Keep it Default
Provide Project Details
Update Gradle File
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
Coroutines != Threads
Couroutines = Light-weight Threads
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
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
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
// 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!")
}
Retrofit?
Client, WebServices, HTTP and Retrofit
HTTP Request
HTTP Response
Client
WebServer
( Web Services )
Client, WebServices, HTTP and Retrofit
HTTP Request
HTTP Response
Client
WebServer
( Web Services )
Retrofit
Lets Do CodeLab
Kotlin + Retrofit
Kotlin Coroutines with Retrofit:
http://bit.ly/kotlin-coroutines-gdg
Kotlin Coroutines Basics:
http://bit.ly/kotlin-coroutines-guide
Just Like Facebook Chat Bubble
➝ Connect with me
• Linkedin.com/in/sriyank
• Instagram/sriyank_smartherd
• Youtube.com/smartherd
• Pluralsight
• Udemy
➝ Sriyank Siddhartha
➝ Smartherd
Sriyank Siddhartha
Thank You!

Contenu connexe

Tendances

Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipseanshunjain
 
Dagger 2 vs koin
Dagger 2 vs koinDagger 2 vs koin
Dagger 2 vs koinJintin Lin
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Mohamed Nabil, MSc.
 
Wicket Next (1.4/1.5)
Wicket Next (1.4/1.5)Wicket Next (1.4/1.5)
Wicket Next (1.4/1.5)jcompagner
 
Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014Oren Rubin
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module PatternsNicholas Jansma
 
Realm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app databaseRealm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app databaseSergi Martínez
 
iOS Memory management & Navigation
iOS Memory management & NavigationiOS Memory management & Navigation
iOS Memory management & NavigationMarian Ignev
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryAlek Davis
 
BangaloreJUG introduction to kotlin
BangaloreJUG   introduction to kotlinBangaloreJUG   introduction to kotlin
BangaloreJUG introduction to kotlinChandra Sekhar Nayak
 
Annotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsAnnotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsJames Kirkbride
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Addy Osmani
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScriptCodemotion
 

Tendances (17)

Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
Dagger 2 vs koin
Dagger 2 vs koinDagger 2 vs koin
Dagger 2 vs koin
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Prototype
PrototypePrototype
Prototype
 
Building gui
Building guiBuilding gui
Building gui
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1
 
Wicket Next (1.4/1.5)
Wicket Next (1.4/1.5)Wicket Next (1.4/1.5)
Wicket Next (1.4/1.5)
 
Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
 
Realm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app databaseRealm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app database
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
iOS Memory management & Navigation
iOS Memory management & NavigationiOS Memory management & Navigation
iOS Memory management & Navigation
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
BangaloreJUG introduction to kotlin
BangaloreJUG   introduction to kotlinBangaloreJUG   introduction to kotlin
BangaloreJUG introduction to kotlin
 
Annotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsAnnotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark Arts
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScript
 

Similaire à Kotlin/Everywhere GDG Bhubaneswar 2019

From Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceFrom Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceStefanTomm
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaDILo Surabaya
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlinAdit Lal
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android developmentAdit Lal
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with AndroidKurt Renzo Acosta
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseChristian Melchior
 
Rapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and KtorRapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and KtorTrayan Iliev
 
droidcon Transylvania - Kotlin Coroutines
droidcon Transylvania - Kotlin Coroutinesdroidcon Transylvania - Kotlin Coroutines
droidcon Transylvania - Kotlin CoroutinesArthur Nagy
 
From Java to Kotlin - The first month in practice v2
From Java to Kotlin - The first month in practice v2From Java to Kotlin - The first month in practice v2
From Java to Kotlin - The first month in practice v2StefanTomm
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyMobileAcademy
 
Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with KotlinDavid Gassner
 
Be More Productive with Kotlin
Be More Productive with KotlinBe More Productive with Kotlin
Be More Productive with KotlinBrandon Wever
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?intelliyole
 
Kotlin, a modern language for modern times
Kotlin, a modern language for modern timesKotlin, a modern language for modern times
Kotlin, a modern language for modern timesSergi Martínez
 
KOIN for dependency Injection
KOIN for dependency InjectionKOIN for dependency Injection
KOIN for dependency InjectionKirill Rozov
 
Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기Taehwan kwon
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android UpdateGarth Gilmour
 

Similaire à Kotlin/Everywhere GDG Bhubaneswar 2019 (20)

From Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceFrom Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practice
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlin
 
Kotlin Generation
Kotlin GenerationKotlin Generation
Kotlin Generation
 
Koin Quickstart
Koin QuickstartKoin Quickstart
Koin Quickstart
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android development
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
 
Rapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and KtorRapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and Ktor
 
droidcon Transylvania - Kotlin Coroutines
droidcon Transylvania - Kotlin Coroutinesdroidcon Transylvania - Kotlin Coroutines
droidcon Transylvania - Kotlin Coroutines
 
From Java to Kotlin - The first month in practice v2
From Java to Kotlin - The first month in practice v2From Java to Kotlin - The first month in practice v2
From Java to Kotlin - The first month in practice v2
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRready
 
Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with Kotlin
 
Be More Productive with Kotlin
Be More Productive with KotlinBe More Productive with Kotlin
Be More Productive with Kotlin
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
Kotlin intro
Kotlin introKotlin intro
Kotlin intro
 
Kotlin, a modern language for modern times
Kotlin, a modern language for modern timesKotlin, a modern language for modern times
Kotlin, a modern language for modern times
 
KOIN for dependency Injection
KOIN for dependency InjectionKOIN for dependency Injection
KOIN for dependency Injection
 
Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 

Dernier

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 

Dernier (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 

Kotlin/Everywhere GDG Bhubaneswar 2019

  • 2. Create Basic Kotlin Project using IntelliJ IDEA
  • 3.
  • 4.
  • 5.
  • 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
  • 7. Just Like Facebook Chat Bubble More preferred than ever…
  • 8. Just Like Facebook Chat Bubble
  • 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
  • 25. Kotlin String Templates Kotlin Feature val firstName = “Sriyank” print(“{$firstName}”) // OUTPUT: Sriyank val lastName = “Siddhartha” print(“$lastName”) // OUTPUT: Siddhartha print(“${firstName} ${lastName}”) // or print(“$firstName $lastName”) // OUTPUT: Sriyank Siddhartha
  • 26. Extension Function Kotlin Collections Scope Functions: let, apply, with, run, also
  • 30. Create Basic Kotlin Project with Coroutines dependencies using IntelliJ IDEA
  • 31.
  • 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
  • 36. Coroutines != Threads Couroutines = Light-weight Threads
  • 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!") }
  • 42. Client, WebServices, HTTP and Retrofit HTTP Request HTTP Response Client WebServer ( Web Services )
  • 43. Client, WebServices, HTTP and Retrofit HTTP Request HTTP Response Client WebServer ( Web Services ) Retrofit
  • 45. Kotlin Coroutines with Retrofit: http://bit.ly/kotlin-coroutines-gdg Kotlin Coroutines Basics: http://bit.ly/kotlin-coroutines-guide
  • 46.
  • 47. Just Like Facebook Chat Bubble ➝ Connect with me • Linkedin.com/in/sriyank • Instagram/sriyank_smartherd • Youtube.com/smartherd • Pluralsight • Udemy ➝ Sriyank Siddhartha ➝ Smartherd Sriyank Siddhartha