Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Kotlin

Avatar for Somaundaram M Somaundaram M
October 28, 2017

Introduction to Kotlin

Used this slides at GDGMumbai devfest for talking about Kotlin Syntax and also contains steps for how to create a simple backend and frontend with Kotlin. #KotlinEverywhere

Avatar for Somaundaram M

Somaundaram M

October 28, 2017

Other Decks in Technology

Transcript

  1. KOTLIN? Where can I use • Anywhere • Android •

    Similar to Java / Javascript / C# / Groovy allows quick ramp time
  2. KOTLIN? Where can I use • Anywhere • Android •

    Similar to Java / Javascript / C# / Groovy allows quick ramp time • Interoperability allows gradual adoption
  3. KOTLIN? How can I use • Command Line • Maven

    • Gradle • Kobalt • Ant
  4. KOTLIN? How can I use • Command Line • Maven

    • Gradle • Kobalt • Ant • Intellij Studio
  5. KOTLIN? How can I use • Command Line • Maven

    • Gradle • Kobalt • Ant • Intellij Studio • Android Studio
  6. KOTLIN? How can I use • Command Line • Maven

    • Gradle • Kobalt • Ant • Intellij Studio • Android Studio • Eclipse
  7. KOTLIN? How can I use • Command Line • Maven

    • Gradle • Kobalt • Ant • Intellij Studio • Android Studio • Eclipse • Netbeans
  8. final int andResult = a & b; final int orResult

    = a | b; final int xorResult = a ^ b; final int rightShift = a >> 2; final int leftShift = a << 2;
  9. val andResult = a and b; val orResult = a

    or b; val xorResult = a xor b; val rightShift = a shr 2; val leftShift = a shl 2;
  10. if (score >= 0 && score <= 300) { }

    // ^^^ Java vvv Kotlin if (score in 0..300) { }
  11. for (int i = 1; i <= 10; i++) {

    } // ^^^ Java vvv Kotlin for (i in 1..10) { }
  12. for (int i = 1; i < 10; i++) {

    } // ^^^ Java vvv Kotlin for (i in 1 until 10) { }
  13. for (int i = 10; i >= 0; i--) {

    } // ^^^ Java vvv Kotlin for (i in 10 downTo 0) { }
  14. for (int i = 1; i <= 10; i+=2) {

    } // ^^^ Java vvv Kotlin for (i in 1..10 step 2) { }
  15. for (int i = 10; i >= 0; i-=2) {

    } // ^^^ Java vvv Kotlin for (i in 10 downTo 1 step 2) { }
  16. for (String item : collection) { } // ^^^ Java

    vvv Kotlin for (item in collection) { }
  17. for (int number : numbers) { if(number > 5) {

    System.out.println(number); } }
  18. class Movie { public String getName() { // .. }

    public void setName(String name) { // .. } } // ^^^ Java vvv Kotlin val movie = Movie() println("Name is " + movie.name)
  19. class Movie { public String getName() { // .. }

    public void setName(String name) { // .. } } // ^^^ Java vvv Kotlin val movie = Movie() println("Name is " + movie.name)
  20. class Movie { public String getName() { // .. }

    public void setName(String name) { // .. } } // ^^^ Java vvv Kotlin val movie = Movie() println("Name is " + movie.name)
  21. class Movie { public String getName() { // .. }

    public void setName(String name) { // .. } } // ^^^ Java vvv Kotlin val movie = Movie() println("Name is ${movie.name})
  22. class Movie { public String getName() { // .. }

    public void setName(String name) { // .. } } // ^^^ Java vvv Kotlin val movie = Movie() println("Name is $movie)
  23. class Movie { var name = "Sholay" } // ^^^

    Kotlin vvv Java Movie movie = new Movie(); System.out.println("Name is " + movie.getName()); movie.setName("Kabali");
  24. class Movie { var name = "Sholay" } // ^^^

    Kotlin vvv Java Movie movie = new Movie(); System.out.println("Name is " + movie.getName()); movie.setName("Kabali");
  25. class Movie { var name = "Sholay" } // ^^^

    Kotlin vvv Java Movie movie = new Movie(); System.out.println("Name is " + movie.getName()); movie.setName("Kabali");
  26. class Movie { var name = "Sholay" } // ^^^

    Kotlin vvv Java Movie movie = new Movie(); System.out.println("Name is " + movie.getName()); movie.setName("Dabang");
  27. fun Date.isReleaseDay(): Boolean { return day == 5 } val

    today = Date() if(today.isReleaseDay()){ println("Today is a release day") }else{ println("Today is not a release day") }
  28. fun Date.isReleaseDay(): Boolean { return day == 5 } val

    today = Date() if(today.isReleaseDay()){ println("Today is a release day") }else{ println("Today is not a release day") }
  29. fun Date.isReleaseDay(): Boolean { return day == 5 } val

    today = Date() if(today.isReleaseDay()){ println("Today is a release day") }else{ println("Today is not a release day") } // ^^^ Kotlin vvv Java DateKt.isReleaseDay(today)
  30. public class Movie { private String name; public Movie(String name)

    { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return “Movie{name=" + name”}”; } }
  31. val ticket = Money(amount = 120) val popcorn = Money(amount

    = 85) val coke = Money(amount = 45) private val Int.m: Money get() { return Money(this, “₹") }
  32. val ticket = 120.m val popcorn = 85.m val coke

    = 45.m private val Int.m: Money get() { return Money(this, “₹") }
  33. val ticket = 120.m val popcorn = 85.m val coke

    = 45.m val total = // ?? fun Money.plus(other: Money): Money { if (currency != other.currency) { throw IllegalArgumentException("Currency Mismatch") } return copy(amount = amount + other.amount) }
  34. val ticket = 120.m val popcorn = 85.m val coke

    = 45.m val total = ticket.plus(popcorn).plus.(coke) fun Money.plus(other: Money): Money { if (currency != other.currency) { throw IllegalArgumentException("Currency Mismatch") } return copy(amount = amount + other.amount) }
  35. val ticket = 120.m val popcorn = 85.m val coke

    = 45.m val total = ticket + popcorn + coke fun Money.plus(other: Money): Money { if (currency != other.currency) { throw IllegalArgumentException("Currency Mismatch") } return copy(amount = amount + other.amount) }
  36. val ticket = 120.m val popcorn = 85.m val coke

    = 45.m val total = ticket + popcorn + coke operator fun Money.plus(other: Money): Money { if (currency != other.currency) { throw IllegalArgumentException("Currency Mismatch") } return copy(amount = amount + other.amount) }
  37. fun convertToRupee(money: Money) : Money { when (money.currency) { "₹"

    -> return money "$" -> return Money(money.amount / 65, "₹") else -> throw // .. } }
  38. fun convertToRupee(money: Money) : Money { when (money.currency) { "₹"

    -> return money "$" -> return Money(money.amount / 65, "₹") else -> throw // .. } }
  39. fun convertToRupee(money: Money) : Money { return when (money.currency) {

    "₹" -> money "$" -> Money(money.amount / 65, "₹") else -> throw // .. } }
  40. fun convertToRupee(money: Money) = when (money.currency) { "₹" -> money

    "$" -> Money(money.amount / 65, "₹") else -> throw // .. }
  41. fun Int.percentOf(money: Money) = money.amount * this / 100 val

    ticket = Money(120, "₹") 28.percentOf(ticket)
  42. fun Int.percentOf(money: Money) = money.amount * this / 100 val

    ticket = Money(120, "₹") 28 percentOf ticket
  43. infix fun Int.percentOf(money: Money) = money.amount * this / 100

    val ticket = Money(120, "₹") 28 percentOf ticket
  44. fun getAllMovies() = arrayListOf( Movie(2016, "Kabali"), Movie(2015, "Bahubali 1"), Movie(2017,

    "Bahubali 2"), Movie(2018, "2.0"), Movie(2017, “Dangal"), //... )
  45. fun getAllMovies() = arrayListOf( Movie(2016, "Kabali"), Movie(2015, "Bahubali 1"), Movie(2017,

    "Bahubali 2"), Movie(2018, "2.0"), Movie(2017, “Dangal"), //… ) val movies = getAllMovies() val movie17 = movies.filter { it.year == 2017 }
  46. fun getAllMovies() = arrayListOf( Movie(2016, "Kabali"), Movie(2015, "Bahubali 1"), Movie(2017,

    "Bahubali 2"), Movie(2018, "2.0"), Movie(2017, “Dangal"), //… ) val movies = getAllMovies() val movie17 = movies.filter { it.year == 2017 }
  47. val tickets = Money(120, "INR") val popcorn = tickets.copy() if

    (tickets == popcorn) { println("They are same in values”) }
  48. val tickets = Money(120, "INR") val popcorn = tickets.copy() if

    (tickets == popcorn) { println("They are same in values”) } if (tickets !== popcorn) { println("They are different in address”) }
  49. Key Features • null safety • not checked exceptions •

    getters & setters • delegations • lateinit
  50. Key Features • null safety • not checked exceptions •

    getters & setters • delegations • lateinit • named & optional arguments
  51. Key Features • null safety • not checked exceptions •

    getters & setters • delegations • lateinit • named & optional arguments • lambdas
  52. Key Features • null safety • not checked exceptions •

    getters & setters • delegations • lateinit • named & optional arguments • lambdas • extension functions
  53. Key Features • null safety • not checked exceptions •

    getters & setters • delegations • lateinit • named & optional arguments • lambdas • extension functions • type aliases
  54. Lets move to web ? • Yes this slide is

    correct, and of-course you are sitting on the mobile track session only.
  55. Lets move to web ? • Yes this slide is

    correct, and of-course you are sitting on the mobile track session only. • Why should only Web developers have all fun.
 ( React-Native eh ? #KotlinEverywhere )
  56. Lets move to web ? • Yes this slide is

    correct, and of-course you are sitting on the mobile track session only. • Why should only Web developers have all fun.
 ( React-Native eh ? #KotlinEverywhere ) • Lets quickly do some web things
  57. fun main(args: Array<String>) { embeddedServer(Netty, 8080) { routing { get("/api/ping/{count?}")

    { var count = Integer.valueOf(call.parameters["count"]?: "1") if (count < 1) { count = 1 } var obj = Array<Entry>(count, {i -> Entry("$i: Hello, World!")}) var gson = Gson() gson = Gson() var str = gson.toJson(obj) call.response.header("Access-Control-Allow-Origin", "*") call.respondText(str, ContentType.Application.Json) } } }.start(wait = true) } data class Entry(val message: String)
  58. <!DOCTYPE html> <!-- index.html --> <html lang="en"> <head> <meta charset="UTF-8">

    <title>Console Output</title> </head> <body> <div style="display:block"> <label for="count_id">Enter number of elements to fetch:</label> <input id="count_id" name="count" value="1"/> </div> <button id="button_id" type="button" style="display:block">Submit</button> <textarea id="textarea_id" style="width: 100%; height: 200px"> </textarea> <script type="text/javascript" src="js/lib/kotlin.js"></script> <script type="text/javascript" src="js/app.js"></script> </body> </html>
  59. fun main(args: Array<String>) { window.onload = { fetch("1") val head

    = document.getElementsByTagName("head") head[0]?.appendChild(createStylesheetLink("style.css")) val input = document.getElementById("count_id") as HTMLInputElement val button = document.getElementById("button_id") button?.addEventListener("click", fun(event: Event) { fetch(input.value) }) } }
  60. fun fetch(count: String): Unit { val url = "http://localhost:8080/api/ping/$count" val

    req = XMLHttpRequest() req.onloadend = fun(event: Event){ val text = req.responseText println(text) val objArray = JSON.parse<Array<Json>>(text) val textarea = document.getElementById("textarea_id") as HTMLTextAreaElement textarea.value = "" objArray.forEach { val message = it["message"] textarea.value += "$message\n" } } req.open("GET", url, true) req.send() }