Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 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(...
#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...
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) {
Sys...
#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
f...
Example : Extension Functions
//Extension Function for Glide
fun ImageView.loadImage(url: String) {
Glide.with(context).lo...
#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 {
lo...
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[] ...
Collections
#Kotlin #Android
Coroutines
#Kotlin #Android
//Coroutine
Threads vs Coroutines
//Threads
#Kotlin #Android
Stacks
Thread 2
Thread 3Thread 1
Thread 1
Coroutine
1
Corouti...
Coroutines
● Light-weight threads
● Stackless coroutine - No mapping on main thread
● No context switching on processor
● ...
Definition : Coroutines
One can think of a coroutine as a light-weight thread. Like threads,
coroutines can run in paralle...
Example : Launch
//Coroutine Launch
fun main(args: Array<String>) {
println("Kotlin Start")
launch(CommonPool) {
delay(200...
Example : Async
// Parallel execution
private fun doWorksInParallel() {
val one = async(CommonPool) {
doWorkFor1Seconds() ...
Anko for Android
#Kotlin #Android
Anko
● Commons: a lightweight library full of helpers for intents, dialogs, logging etc.
● Layouts: a fast and type-safe w...
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("...
Example : Layouts
//using Anko to create layouts
verticalLayout {
val name = editText()
button("Say Hello") {
onClick { to...
Example : SQLite
//using Anko to create table
database.use {
createTable("User", true,
"id" to INTEGER + PRIMARY_KEY,
"nam...
Example : Coroutines
//using Anko to create async task
fun authorizeUser(username: String, password: String) {
doAsync {
v...
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 Dev...
#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://w...
Upcoming SlideShare
Loading in …5
×

Fall in love with Kotlin

1,764 views

Published on

The following deck was presented at GDG DevFest 17.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Fall in love with Kotlin

  1. 1. Fall in love with Kotlin #Kotlin #Android Hari Vignesh Jayapalan Android | UX Engineer
  2. 2. Now Official for Android #Kotlin #Android
  3. 3. Kotlin is a statically typed programming language for the JVM, Android and the browser. #Kotlin #Android
  4. 4. Session : Today ● Kotlin Features ● Advanced Core Features ● Anko for Android ● Migrating to Kotlin ● Drawbacks of Kotlin #Kotlin #Android
  5. 5. Kotlin Features #Kotlin #Android
  6. 6. #Kotlin #Android #1 Concise Reduced Boilerplate Code
  7. 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. 8. #Kotlin #Android #2 Safe Avoid Null-Pointer Exceptions
  9. 9. Example : Safe //Won’t Compile //Compiles #Kotlin #Android var notNullEvent: Event = null var event: Event? = null
  10. 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. 11. Example : Safe #Kotlin #Android //Elvis Operator val name = event?.name ?: "empty” // !! Operator val l = b!!.length
  12. 12. #Kotlin #Android #3 Lambdas Functional Support
  13. 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. 14. #Kotlin #Android #4 Extension Functions Add new functions to any Class
  15. 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. 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. 17. #Kotlin #Android #5 Higher Order Functions Return functions & functions as parameters
  18. 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
  19. 19. Core Features #Kotlin #Android
  20. 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
  21. 21. Collections #Kotlin #Android
  22. 22. Coroutines #Kotlin #Android
  23. 23. //Coroutine Threads vs Coroutines //Threads #Kotlin #Android Stacks Thread 2 Thread 3Thread 1 Thread 1 Coroutine 1 Coroutine 2 Coroutine 3
  24. 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. 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. 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. 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
  28. 28. Anko for Android #Kotlin #Android
  29. 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. 30. Anko Commons ● Intents ● Dialogs and toasts ● Logging ● Resources and dimensions #Kotlin #Android
  31. 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. 32. Example : Layouts //using Anko to create layouts verticalLayout { val name = editText() button("Say Hello") { onClick { toast("Hello, ${name.text}!") } } } //Output #Kotlin #Android
  33. 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. 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
  35. 35. Lot More #Kotlin #Android
  36. 36. Migrating to Kotlin #Kotlin #Android
  37. 37. Drawbacks of Kotlin #Kotlin #Android
  38. 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. 39. #Kotlin #Android Let’s stay in touch Google “Hari Vignesh Jayapalan” Thank you!
  40. 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

×