SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Fall in love with Kotlin
#Kotlin #Android
Hari Vignesh Jayapalan
Android | UX Engineer
Now Official for Android
#Kotlin #Android
Kotlin is a statically typed
programming language for the JVM,
Android and the browser.
#Kotlin #Android
Session : Today
● Kotlin Features
● Advanced Core Features
● Anko for Android
● Migrating to Kotlin
● Drawbacks of Kotlin
#Kotlin #Android
Kotlin Features
#Kotlin #Android
#Kotlin #Android
#1 Concise
Reduced Boilerplate Code
Example : Concise
public class Event { //Java
private long id;
private String name;
private String url;
public long getId() {...}
public String getName() {...}
public String getUrl() {...}
public void setId(long id) {...}
public void setName(String name) {...}
public void setUrl(String url) {...}
@override public String toString(){...}
}
//Kotlin
data class Event(
var id: Long,
var name: String,
var url: String)
#Kotlin #Android
#Kotlin #Android
#2 Safe
Avoid Null-Pointer Exceptions
Example : Safe
//Won’t Compile //Compiles
#Kotlin #Android
var notNullEvent: Event = null var event: Event? = null
Example : Safe
#Kotlin #Android
//Null validation Java
if (text != null) {
int length = text.length();
}
//Null validation kotlin
text?.let {
val length = text.length
}
//or simply
val length = text?.length
Example : Safe
#Kotlin #Android
//Elvis Operator
val name = event?.name ?: "empty”
// !! Operator
val l = b!!.length
#Kotlin #Android
#3 Lambdas
Functional Support
Example : Lambdas
//Java
view.setOnClickListener( new
View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Button Clicked");
}
});
//Kotlin
view.setOnClickListener {
println("Hello world!")
}
#Kotlin #Android
#Kotlin #Android
#4 Extension Functions
Add new functions to any Class
Example : Extension Functions
//Normal Kotlin Function
fun triple(): Int {
return this * 3
}
//Extension Kotlin Function
fun Int.triple(): Int {
return this * 3
}
//Using Extension Function
var result = 3.triple()
#Kotlin #Android
Example : Extension Functions
//Extension Function for Glide
fun ImageView.loadImage(url: String) {
Glide.with(context).load(url).into(this)
}
//Usage
imageView.loadImage(url)
#Kotlin #Android
#Kotlin #Android
#5 Higher Order Functions
Return functions & functions as parameters
Example : Higher Order Functions
//Takes body() as parameter and returns T
fun <T> lock(lock: Lock, body: () -> T): T {
lock.lock()
try {
return body()
}
finally {
lock.unlock()
}
}
#Kotlin #Android
Core Features
#Kotlin #Android
//Kotlin collections
val list = listOf(1, 2, 3, 4, 5, 6)
println(list.filter{it%2==0})
Example : Collections
//Java
int[] list={1,2,3,4,5,6};
ArrayList<Integer> list2=new ArrayList<>();
for(int it:list){
if(it%2==0){
list2.add(it);
}
}
#Kotlin #Android
Collections
#Kotlin #Android
Coroutines
#Kotlin #Android
//Coroutine
Threads vs Coroutines
//Threads
#Kotlin #Android
Stacks
Thread 2
Thread 3Thread 1
Thread 1
Coroutine
1
Coroutine
2
Coroutine
3
Coroutines
● Light-weight threads
● Stackless coroutine - No mapping on main thread
● No context switching on processor
● Multitasking and managed by user
#Kotlin #Android
Definition : Coroutines
One can think of a coroutine as a light-weight thread. Like threads,
coroutines can run in parallel, wait for each other and communicate.
The biggest difference is that coroutines are very cheap, almost free:
we can create thousands of them, and pay very little in terms of
performance. True threads, on the other hand, are expensive to start
and keep around. A thousand threads can be a serious challenge for a
modern machine.
- kotlinlang.org
#Kotlin #Android
Example : Launch
//Coroutine Launch
fun main(args: Array<String>) {
println("Kotlin Start")
launch(CommonPool) {
delay(2000)
println("Kotlin Inside")
}
println("Kotlin End")
}
// The output will be
// Kotlin Start
// Kotlin End
// Kotlin Inside
#Kotlin #Android
Example : Async
// Parallel execution
private fun doWorksInParallel() {
val one = async(CommonPool) {
doWorkFor1Seconds() }
val two = async(CommonPool) {
doWorkFor2Seconds()}
launch(CommonPool) {
val combined = one.await() + "_" + two.await()
println("Kotlin Combined : " + combined)
}
}
suspend fun doWorkFor1Seconds(): String {
delay(1000)
return “doWorkFor1Seconds” }
suspend fun doWorkFor2Seconds(): String {
delay(2000)
return “doWorkFor2Seconds” }
#Kotlin #Android
// The output is
// Kotlin Combined :
//doWorkFor1Seconds_doWorkFor2Seconds
Anko for Android
#Kotlin #Android
Anko
● Commons: a lightweight library full of helpers for intents, dialogs, logging etc.
● Layouts: a fast and type-safe way to write dynamic Android layouts
● SQLite: a query DSL and parser collection for Android SQLite
● Coroutines: utilities based on the kotlinx.coroutines library
#Kotlin #Android
Anko Commons
● Intents
● Dialogs and toasts
● Logging
● Resources and dimensions
#Kotlin #Android
Example : Commons
//Calling Intent using Kotlin
val intent = Intent(this,
SomeOtherActivity::class.java)
intent.putExtra("id", 5)
intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_
TOP)
startActivity(intent)
//Calling Intent using Anko Commons
startActivity(intentFor<SomeOtherActivity>("id"
to 5).singleTop())
//Calling Intent using Anko Commons
startActivity(intentFor<SomeOtherActivity>("id"
to 5,"name" to "Hari"))
#Kotlin #Android
Example : Layouts
//using Anko to create layouts
verticalLayout {
val name = editText()
button("Say Hello") {
onClick { toast("Hello, ${name.text}!") }
}
}
//Output
#Kotlin #Android
Example : SQLite
//using Anko to create table
database.use {
createTable("User", true,
"id" to INTEGER + PRIMARY_KEY,
"name" to TEXT,
"photo" to BLOB)
}
//using Anko to query
db.select("User", "name")
.whereArgs("(_id > {userId}) and (name =
{userName})",
"userName" to "John",
"userId" to 42)
#Kotlin #Android
Example : Coroutines
//using Anko to create async task
fun authorizeUser(username: String, password: String) {
doAsync {
val authorized = signInBL.checkUserCredentials(
AuthCredentials(username = username, password = password))
activityUiThread {
if (authorized) {
toast("Signed!!!")
} else {
view.showAccessDeniedAlertDialog()
}
}
}
#Kotlin #Android
Lot More
#Kotlin #Android
Migrating to Kotlin
#Kotlin #Android
Drawbacks of Kotlin
#Kotlin #Android
Other Resources
● Mindorks - http://blog.mindorks.com/
● Kotlin Official - https://kotlinlang.org
● Kotlin for Android Developers - https://antonioleiva.com
● Kotlin Weekly - http://www.kotlinweekly.net
● PacktPub - https://www.packtpub.com/books/content/what-kotlin
● Kotlin Koans - https://kotlinlang.org/docs/tutorials/koans.html
#Kotlin #Android
#Kotlin #Android
Let’s stay in touch
Google “Hari Vignesh Jayapalan”
Thank you!
Content Credits
● Mindorks - http://blog.mindorks.com/
● Kotlin Official - https://kotlinlang.org
● Word Cloud - https://worditout.com/
● Kotlin for Android Developers - https://antonioleiva.com
#Kotlin #Android

Weitere ähnliche Inhalte

Was ist angesagt?

Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to KotlinMagda Miu
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentAdam Magaña
 
Getting Started With Kotlin
Getting Started With KotlinGetting Started With Kotlin
Getting Started With KotlinGaurav sharma
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvmArnaud Giuliani
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation MobileAcademy
 
Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)Hassan Abid
 
Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Arnaud Giuliani
 
Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Andrey Breslav
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show fileSaurabh Tripathi
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformEastBanc Tachnologies
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesAndroid 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesKai Koenig
 
What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?Squareboat
 
Kotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoKotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoArnaud Giuliani
 
A short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developersA short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developersAntonis Lilis
 
BangaloreJUG introduction to kotlin
BangaloreJUG   introduction to kotlinBangaloreJUG   introduction to kotlin
BangaloreJUG introduction to kotlinChandra Sekhar Nayak
 

Was ist angesagt? (20)

Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
 
Android with kotlin course
Android with kotlin courseAndroid with kotlin course
Android with kotlin course
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android Development
 
Getting Started With Kotlin
Getting Started With KotlinGetting Started With Kotlin
Getting Started With Kotlin
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)
 
Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017
 
Kotlin Overview
Kotlin OverviewKotlin Overview
Kotlin Overview
 
Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?
 
Coding in kotlin
Coding in kotlinCoding in kotlin
Coding in kotlin
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesAndroid 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutes
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?
 
Kotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoKotlin cheat sheet by ekito
Kotlin cheat sheet by ekito
 
A short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developersA short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developers
 
BangaloreJUG introduction to kotlin
BangaloreJUG   introduction to kotlinBangaloreJUG   introduction to kotlin
BangaloreJUG introduction to kotlin
 

Ähnlich wie Fall in love with Kotlin

2#Kotlin programming tutorials(data types and hello world)
2#Kotlin programming tutorials(data types and hello world)2#Kotlin programming tutorials(data types and hello world)
2#Kotlin programming tutorials(data types and hello world)Naveen Davis
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android UpdateGarth Gilmour
 
Android Application Development (1).pptx
Android Application Development (1).pptxAndroid Application Development (1).pptx
Android Application Development (1).pptxadityakale2110
 
Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with KotlinDavid Gassner
 
TypeScript Vs. KotlinJS
TypeScript Vs. KotlinJSTypeScript Vs. KotlinJS
TypeScript Vs. KotlinJSGarth Gilmour
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseChristian Melchior
 
Geecon - Improve your Android-fu with Kotlin
Geecon - Improve your Android-fu with KotlinGeecon - Improve your Android-fu with Kotlin
Geecon - Improve your Android-fu with KotlinNicolas Fränkel
 
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveWriting Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveAndré Oriani
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Tudor Dragan
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#Talbott Crowell
 
MOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxMOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxkamalkantmaurya1
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?intelliyole
 
Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Omar Miatello
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyMobileAcademy
 
Kotlin for Android devs
Kotlin for Android devsKotlin for Android devs
Kotlin for Android devsAdit Lal
 
fun kotlinMultiplatform()
fun kotlinMultiplatform()fun kotlinMultiplatform()
fun kotlinMultiplatform()ssuserdd678d
 
Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?Kai Koenig
 

Ähnlich wie Fall in love with Kotlin (20)

Koin Quickstart
Koin QuickstartKoin Quickstart
Koin Quickstart
 
2#Kotlin programming tutorials(data types and hello world)
2#Kotlin programming tutorials(data types and hello world)2#Kotlin programming tutorials(data types and hello world)
2#Kotlin programming tutorials(data types and hello world)
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 
Android Application Development (1).pptx
Android Application Development (1).pptxAndroid Application Development (1).pptx
Android Application Development (1).pptx
 
Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with Kotlin
 
TypeScript Vs. KotlinJS
TypeScript Vs. KotlinJSTypeScript Vs. KotlinJS
TypeScript Vs. KotlinJS
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
 
Geecon - Improve your Android-fu with Kotlin
Geecon - Improve your Android-fu with KotlinGeecon - Improve your Android-fu with Kotlin
Geecon - Improve your Android-fu with Kotlin
 
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveWriting Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
 
從零開始學 Android
從零開始學 Android從零開始學 Android
從零開始學 Android
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#
 
MOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxMOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptx
 
moocs_ppt.pptx
moocs_ppt.pptxmoocs_ppt.pptx
moocs_ppt.pptx
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRready
 
Kotlin for Android devs
Kotlin for Android devsKotlin for Android devs
Kotlin for Android devs
 
fun kotlinMultiplatform()
fun kotlinMultiplatform()fun kotlinMultiplatform()
fun kotlinMultiplatform()
 
Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?
 

Kürzlich hochgeladen

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Kürzlich hochgeladen (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Fall in love with Kotlin

  • 1. Fall in love with Kotlin #Kotlin #Android Hari Vignesh Jayapalan Android | UX Engineer
  • 2. Now Official for Android #Kotlin #Android
  • 3. Kotlin is a statically typed programming language for the JVM, Android and the browser. #Kotlin #Android
  • 4. Session : Today ● Kotlin Features ● Advanced Core Features ● Anko for Android ● Migrating to Kotlin ● Drawbacks of Kotlin #Kotlin #Android
  • 7. Example : Concise public class Event { //Java private long id; private String name; private String url; public long getId() {...} public String getName() {...} public String getUrl() {...} public void setId(long id) {...} public void setName(String name) {...} public void setUrl(String url) {...} @override public String toString(){...} } //Kotlin data class Event( var id: Long, var name: String, var url: String) #Kotlin #Android
  • 8. #Kotlin #Android #2 Safe Avoid Null-Pointer Exceptions
  • 9. Example : Safe //Won’t Compile //Compiles #Kotlin #Android var notNullEvent: Event = null var event: Event? = null
  • 10. Example : Safe #Kotlin #Android //Null validation Java if (text != null) { int length = text.length(); } //Null validation kotlin text?.let { val length = text.length } //or simply val length = text?.length
  • 11. Example : Safe #Kotlin #Android //Elvis Operator val name = event?.name ?: "empty” // !! Operator val l = b!!.length
  • 13. Example : Lambdas //Java view.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("Button Clicked"); } }); //Kotlin view.setOnClickListener { println("Hello world!") } #Kotlin #Android
  • 14. #Kotlin #Android #4 Extension Functions Add new functions to any Class
  • 15. Example : Extension Functions //Normal Kotlin Function fun triple(): Int { return this * 3 } //Extension Kotlin Function fun Int.triple(): Int { return this * 3 } //Using Extension Function var result = 3.triple() #Kotlin #Android
  • 16. Example : Extension Functions //Extension Function for Glide fun ImageView.loadImage(url: String) { Glide.with(context).load(url).into(this) } //Usage imageView.loadImage(url) #Kotlin #Android
  • 17. #Kotlin #Android #5 Higher Order Functions Return functions & functions as parameters
  • 18. Example : Higher Order Functions //Takes body() as parameter and returns T fun <T> lock(lock: Lock, body: () -> T): T { lock.lock() try { return body() } finally { lock.unlock() } } #Kotlin #Android
  • 20. //Kotlin collections val list = listOf(1, 2, 3, 4, 5, 6) println(list.filter{it%2==0}) Example : Collections //Java int[] list={1,2,3,4,5,6}; ArrayList<Integer> list2=new ArrayList<>(); for(int it:list){ if(it%2==0){ list2.add(it); } } #Kotlin #Android
  • 23. //Coroutine Threads vs Coroutines //Threads #Kotlin #Android Stacks Thread 2 Thread 3Thread 1 Thread 1 Coroutine 1 Coroutine 2 Coroutine 3
  • 24. Coroutines ● Light-weight threads ● Stackless coroutine - No mapping on main thread ● No context switching on processor ● Multitasking and managed by user #Kotlin #Android
  • 25. Definition : Coroutines One can think of a coroutine as a light-weight thread. Like threads, coroutines can run in parallel, wait for each other and communicate. The biggest difference is that coroutines are very cheap, almost free: we can create thousands of them, and pay very little in terms of performance. True threads, on the other hand, are expensive to start and keep around. A thousand threads can be a serious challenge for a modern machine. - kotlinlang.org #Kotlin #Android
  • 26. Example : Launch //Coroutine Launch fun main(args: Array<String>) { println("Kotlin Start") launch(CommonPool) { delay(2000) println("Kotlin Inside") } println("Kotlin End") } // The output will be // Kotlin Start // Kotlin End // Kotlin Inside #Kotlin #Android
  • 27. Example : Async // Parallel execution private fun doWorksInParallel() { val one = async(CommonPool) { doWorkFor1Seconds() } val two = async(CommonPool) { doWorkFor2Seconds()} launch(CommonPool) { val combined = one.await() + "_" + two.await() println("Kotlin Combined : " + combined) } } suspend fun doWorkFor1Seconds(): String { delay(1000) return “doWorkFor1Seconds” } suspend fun doWorkFor2Seconds(): String { delay(2000) return “doWorkFor2Seconds” } #Kotlin #Android // The output is // Kotlin Combined : //doWorkFor1Seconds_doWorkFor2Seconds
  • 29. Anko ● Commons: a lightweight library full of helpers for intents, dialogs, logging etc. ● Layouts: a fast and type-safe way to write dynamic Android layouts ● SQLite: a query DSL and parser collection for Android SQLite ● Coroutines: utilities based on the kotlinx.coroutines library #Kotlin #Android
  • 30. Anko Commons ● Intents ● Dialogs and toasts ● Logging ● Resources and dimensions #Kotlin #Android
  • 31. Example : Commons //Calling Intent using Kotlin val intent = Intent(this, SomeOtherActivity::class.java) intent.putExtra("id", 5) intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_ TOP) startActivity(intent) //Calling Intent using Anko Commons startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop()) //Calling Intent using Anko Commons startActivity(intentFor<SomeOtherActivity>("id" to 5,"name" to "Hari")) #Kotlin #Android
  • 32. Example : Layouts //using Anko to create layouts verticalLayout { val name = editText() button("Say Hello") { onClick { toast("Hello, ${name.text}!") } } } //Output #Kotlin #Android
  • 33. Example : SQLite //using Anko to create table database.use { createTable("User", true, "id" to INTEGER + PRIMARY_KEY, "name" to TEXT, "photo" to BLOB) } //using Anko to query db.select("User", "name") .whereArgs("(_id > {userId}) and (name = {userName})", "userName" to "John", "userId" to 42) #Kotlin #Android
  • 34. Example : Coroutines //using Anko to create async task fun authorizeUser(username: String, password: String) { doAsync { val authorized = signInBL.checkUserCredentials( AuthCredentials(username = username, password = password)) activityUiThread { if (authorized) { toast("Signed!!!") } else { view.showAccessDeniedAlertDialog() } } } #Kotlin #Android
  • 38. Other Resources ● Mindorks - http://blog.mindorks.com/ ● Kotlin Official - https://kotlinlang.org ● Kotlin for Android Developers - https://antonioleiva.com ● Kotlin Weekly - http://www.kotlinweekly.net ● PacktPub - https://www.packtpub.com/books/content/what-kotlin ● Kotlin Koans - https://kotlinlang.org/docs/tutorials/koans.html #Kotlin #Android
  • 39. #Kotlin #Android Let’s stay in touch Google “Hari Vignesh Jayapalan” Thank you!
  • 40. Content Credits ● Mindorks - http://blog.mindorks.com/ ● Kotlin Official - https://kotlinlang.org ● Word Cloud - https://worditout.com/ ● Kotlin for Android Developers - https://antonioleiva.com #Kotlin #Android

Hinweis der Redaktion

  1. Also, supports native LLVM. Kotlin is a new programming language from JetBrains, the maker of the world’s best IDEs.
  2. Drastically reduce the amount of boilerplate code you need to write.
  3. Also get hashCode and equals for free
  4. Use Elvis operator to give an alternative in case the object is null 2. Thus, if you want an NPE, you can have it, but you have to ask for it explicitly, and it does not appear out of the blue.
  5. Kotlin is basically an object oriented language, not a pure functional language. However, as many other modern languages, it uses many concepts from functional programming, such as lambda expressions, to solve some problems in a much easier way.
  6. A higher-order function is a function that takes functions as parameters, or returns a function
  7. Coroutines are based on the idea of suspending functions: functions that can stop the execution when they are called and make it continue once it has finished running their background task. Suspending functions are marked with the reserved word suspend, and can only be called inside other suspending functions or inside a coroutine. Coroutines are a new way of writing asynchronous, non-blocking code (and much more)
  8. Also get hashCode and equals for free
  9. This starts a new coroutine on a given thread pool. In this case, we are using CommonPool that uses ForkJoinPool.commonPool(). Threads still exist in a program based on coroutines, but one thread can run many coroutines, so there’s no need for too many threads. If you directly do this in the main function, it says this with error: Suspend functions are only allowed to be called from a coroutine or another suspend function. Here, the delay function is suspend function, so we can only call it from a coroutine or an another suspend function.
  10. Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.
  11. Talk about Rx Kotlin Talk about interoperability Talk about Kotlin plugin Convincing Management How to start learning? Start with test case
  12. Method count Code converter Build speed Not purely functional, internal conversion is more Learning curve Smaller community and less experts