SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
Kotlin CoroutinesKotlin Fundamentals
Kotlin/Everywhere
Create Basic Kotlin Project using
IntelliJ IDEA
Kotlin/Everywhere GDG Bhubaneswar 2019
Kotlin/Everywhere GDG Bhubaneswar 2019
Kotlin/Everywhere GDG Bhubaneswar 2019
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
Kotlin/Everywhere GDG Bhubaneswar 2019
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
Kotlin/Everywhere GDG Bhubaneswar 2019
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!

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Similar a 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
 

Similar a 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
 

Último

Cybersecurity Challenges with Generative AI - for Good and Bad
Cybersecurity Challenges with Generative AI - for Good and BadCybersecurity Challenges with Generative AI - for Good and Bad
Cybersecurity Challenges with Generative AI - for Good and BadIvo Andreev
 
Kawika Technologies pvt ltd Software Development Company in Trivandrum
Kawika Technologies pvt ltd Software Development Company in TrivandrumKawika Technologies pvt ltd Software Development Company in Trivandrum
Kawika Technologies pvt ltd Software Development Company in TrivandrumKawika Technologies
 
Watermarking in Source Code: Applications and Security Challenges
Watermarking in Source Code: Applications and Security ChallengesWatermarking in Source Code: Applications and Security Challenges
Watermarking in Source Code: Applications and Security ChallengesShyamsundar Das
 
Your Vision, Our Expertise: TECUNIQUE's Tailored Software Teams
Your Vision, Our Expertise: TECUNIQUE's Tailored Software TeamsYour Vision, Our Expertise: TECUNIQUE's Tailored Software Teams
Your Vision, Our Expertise: TECUNIQUE's Tailored Software TeamsJaydeep Chhasatia
 
Why Choose Brain Inventory For Ecommerce Development.pdf
Why Choose Brain Inventory For Ecommerce Development.pdfWhy Choose Brain Inventory For Ecommerce Development.pdf
Why Choose Brain Inventory For Ecommerce Development.pdfBrain Inventory
 
OpenChain Webinar: Universal CVSS Calculator
OpenChain Webinar: Universal CVSS CalculatorOpenChain Webinar: Universal CVSS Calculator
OpenChain Webinar: Universal CVSS CalculatorShane Coughlan
 
Top Software Development Trends in 2024
Top Software Development Trends in  2024Top Software Development Trends in  2024
Top Software Development Trends in 2024Mind IT Systems
 
Deep Learning for Images with PyTorch - Datacamp
Deep Learning for Images with PyTorch - DatacampDeep Learning for Images with PyTorch - Datacamp
Deep Learning for Images with PyTorch - DatacampVICTOR MAESTRE RAMIREZ
 
Generative AI for Cybersecurity - EC-Council
Generative AI for Cybersecurity - EC-CouncilGenerative AI for Cybersecurity - EC-Council
Generative AI for Cybersecurity - EC-CouncilVICTOR MAESTRE RAMIREZ
 
ARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdf
ARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdfARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdf
ARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdfTobias Schneck
 
eAuditor Audits & Inspections - conduct field inspections
eAuditor Audits & Inspections - conduct field inspectionseAuditor Audits & Inspections - conduct field inspections
eAuditor Audits & Inspections - conduct field inspectionsNirav Modi
 
IA Generativa y Grafos de Neo4j: RAG time
IA Generativa y Grafos de Neo4j: RAG timeIA Generativa y Grafos de Neo4j: RAG time
IA Generativa y Grafos de Neo4j: RAG timeNeo4j
 
Webinar - IA generativa e grafi Neo4j: RAG time!
Webinar - IA generativa e grafi Neo4j: RAG time!Webinar - IA generativa e grafi Neo4j: RAG time!
Webinar - IA generativa e grafi Neo4j: RAG time!Neo4j
 
Kubernetes go-live checklist for your microservices.pptx
Kubernetes go-live checklist for your microservices.pptxKubernetes go-live checklist for your microservices.pptx
Kubernetes go-live checklist for your microservices.pptxPrakarsh -
 
About .NET 8 and a first glimpse into .NET9
About .NET 8 and a first glimpse into .NET9About .NET 8 and a first glimpse into .NET9
About .NET 8 and a first glimpse into .NET9Jürgen Gutsch
 
Big Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/ML
Big Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/MLBig Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/ML
Big Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/MLAlluxio, Inc.
 
Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...
Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...
Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...Jaydeep Chhasatia
 
Growing Oxen: channel operators and retries
Growing Oxen: channel operators and retriesGrowing Oxen: channel operators and retries
Growing Oxen: channel operators and retriesSoftwareMill
 
Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...
Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...
Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...OnePlan Solutions
 

Último (20)

Cybersecurity Challenges with Generative AI - for Good and Bad
Cybersecurity Challenges with Generative AI - for Good and BadCybersecurity Challenges with Generative AI - for Good and Bad
Cybersecurity Challenges with Generative AI - for Good and Bad
 
Program with GUTs
Program with GUTsProgram with GUTs
Program with GUTs
 
Kawika Technologies pvt ltd Software Development Company in Trivandrum
Kawika Technologies pvt ltd Software Development Company in TrivandrumKawika Technologies pvt ltd Software Development Company in Trivandrum
Kawika Technologies pvt ltd Software Development Company in Trivandrum
 
Watermarking in Source Code: Applications and Security Challenges
Watermarking in Source Code: Applications and Security ChallengesWatermarking in Source Code: Applications and Security Challenges
Watermarking in Source Code: Applications and Security Challenges
 
Your Vision, Our Expertise: TECUNIQUE's Tailored Software Teams
Your Vision, Our Expertise: TECUNIQUE's Tailored Software TeamsYour Vision, Our Expertise: TECUNIQUE's Tailored Software Teams
Your Vision, Our Expertise: TECUNIQUE's Tailored Software Teams
 
Why Choose Brain Inventory For Ecommerce Development.pdf
Why Choose Brain Inventory For Ecommerce Development.pdfWhy Choose Brain Inventory For Ecommerce Development.pdf
Why Choose Brain Inventory For Ecommerce Development.pdf
 
OpenChain Webinar: Universal CVSS Calculator
OpenChain Webinar: Universal CVSS CalculatorOpenChain Webinar: Universal CVSS Calculator
OpenChain Webinar: Universal CVSS Calculator
 
Top Software Development Trends in 2024
Top Software Development Trends in  2024Top Software Development Trends in  2024
Top Software Development Trends in 2024
 
Deep Learning for Images with PyTorch - Datacamp
Deep Learning for Images with PyTorch - DatacampDeep Learning for Images with PyTorch - Datacamp
Deep Learning for Images with PyTorch - Datacamp
 
Generative AI for Cybersecurity - EC-Council
Generative AI for Cybersecurity - EC-CouncilGenerative AI for Cybersecurity - EC-Council
Generative AI for Cybersecurity - EC-Council
 
ARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdf
ARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdfARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdf
ARM Talk @ Rejekts - Will ARM be the new Mainstream in our Data Centers_.pdf
 
eAuditor Audits & Inspections - conduct field inspections
eAuditor Audits & Inspections - conduct field inspectionseAuditor Audits & Inspections - conduct field inspections
eAuditor Audits & Inspections - conduct field inspections
 
IA Generativa y Grafos de Neo4j: RAG time
IA Generativa y Grafos de Neo4j: RAG timeIA Generativa y Grafos de Neo4j: RAG time
IA Generativa y Grafos de Neo4j: RAG time
 
Webinar - IA generativa e grafi Neo4j: RAG time!
Webinar - IA generativa e grafi Neo4j: RAG time!Webinar - IA generativa e grafi Neo4j: RAG time!
Webinar - IA generativa e grafi Neo4j: RAG time!
 
Kubernetes go-live checklist for your microservices.pptx
Kubernetes go-live checklist for your microservices.pptxKubernetes go-live checklist for your microservices.pptx
Kubernetes go-live checklist for your microservices.pptx
 
About .NET 8 and a first glimpse into .NET9
About .NET 8 and a first glimpse into .NET9About .NET 8 and a first glimpse into .NET9
About .NET 8 and a first glimpse into .NET9
 
Big Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/ML
Big Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/MLBig Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/ML
Big Data Bellevue Meetup | Enhancing Python Data Loading in the Cloud for AI/ML
 
Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...
Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...
Optimizing Business Potential: A Guide to Outsourcing Engineering Services in...
 
Growing Oxen: channel operators and retries
Growing Oxen: channel operators and retriesGrowing Oxen: channel operators and retries
Growing Oxen: channel operators and retries
 
Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...
Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...
Transforming PMO Success with AI - Discover OnePlan Strategic Portfolio Work ...
 

Kotlin/Everywhere GDG Bhubaneswar 2019

  • 2. Create Basic Kotlin Project using IntelliJ IDEA
  • 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
  • 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
  • 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