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

A Dive into Kotlin.

A Dive into Kotlin.

A basic intro into the Kotlin Programming language and it's subtle yet powerful features over Java.

Ashish Krishnan

April 28, 2018
Tweet

More Decks by Ashish Krishnan

Other Decks in Programming

Transcript

  1. Kotlin 1. Statically - typed 2. Runs on JVM 3.

    Production ready 4. Expressive 5. Pragmatic 6. Open Source
  2. Kotlin 1. Statically - typed 2. Runs on JVM 3.

    Production ready 4. Expressive 5. Pragmatic 6. Open Source
  3. Kotlin 1. Statically - typed 2. Runs on JVM 3.

    Production ready 4. Expressive 5. Pragmatic 6. Open Source
  4. Kotlin 1. Statically - typed 2. Runs on JVM 3.

    Production ready 4. Expressive 5. Pragmatic 6. Open Source
  5. Kotlin 1. Statically - typed 2. Runs on JVM 3.

    Production ready 4. Expressive 5. Pragmatic 6. Open Source
  6. Kotlin 1. Statically - typed 2. Runs on JVM 3.

    Production ready 4. Expressive 5. Pragmatic 6. Open Source (~600k) https://github.com/JetBrains/kotlin
  7. public class Person { String name; int age; String email;

    public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; }
  8. public class Person { String name; int age; String email;

    public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; }
  9. public class Person { String name; int age; String email;

    public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } public String getName() { // } public void setName(String name) { // }
  10. @Override public boolean equals(Object o) { if (this == o)

    return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; // more code } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; // return result; } @Override public String toString() { return ; // more code } }
  11. @Override public boolean equals(Object o) { if (this == o)

    return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; // more code } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; // return result; } @Override public String toString() { return ; // more code } }
  12. @Override public boolean equals(Object o) { if (this == o)

    return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; // more code } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; // return result; } @Override public String toString() { return ; // more code } }
  13. data class Person( val name: String, val age: Int, val

    company: String ) Short & Sweet - getters - setters - equals() - hashCode() - toString() - constructors
  14. Talking to Java fun main(args: Array<String>) { val customer =

    CustomerJava() customer.email = "[email protected]" val runnable = Runnable { println("Invoking runnable") } val kr = KotlinCustomerRepo() val customerJava = kr.getById(10) }
  15. Talking to Java fun main(args: Array<String>) { val customer =

    CustomerJava() customer.email = "[email protected]" val runnable = Runnable { println("Invoking runnable") } val kr = KotlinCustomerRepo() val customerJava = kr.getById(10) }
  16. Talking to Java fun main(args: Array<String>) { val customer =

    CustomerJava() customer.email = "[email protected]" val runnable = Runnable { println("Invoking runnable") } val kr = KotlinCustomerRepo() val customerJava = kr.getById(10) }
  17. public static void main(String[] args) { CustomerKotlin customerKotlin = new

    CustomerKotlin(1, "Ashish", "[email protected]"); customerKotlin.setEmail(“[email protected]"); } Talking to Kotlin
  18. public static void main(String[] args) { CustomerKotlin customerKotlin = new

    CustomerKotlin(1, "Ashish", "[email protected]"); customerKotlin.setEmail(“[email protected]"); } Talking to Kotlin
  19. var name: String = "kotlin" name = null // compiler

    is not happy var name: String? = "kotlin" name = null // happy Dealing with Nulls
  20. var name: String = "kotlin" name = null // compiler

    is not happy var name: String? = "kotlin" name = null // happy Dealing with Nulls
  21. var name: String = "kotlin" name = null // compiler

    is not happy var name: String? = "kotlin" name = null // happy name?.length -> Safe call Dealing with Nulls
  22. val length = name?.length ?: -1 -> Elvis Operator val

    length = name!!.length -> For NPE Lovers, not-null assertion Dealing with Nulls
  23. String Interpolation String name = "Ashish"; String company = "Kite";

    System.out.println("Hi, I am " + name + "and I fly Android at " + company);
  24. String Interpolation val name = "Ashish"; val company = "Kite";

    println("Hi, I am $name and I fly Android at $company");
  25. when when (number) { 1 -> print("number is 1") 2

    -> print("number is 2") 3, 4 -> print("number is 3 or 4") in 5 .. 10 -> print("number is 5, 6, 7, 8, 9, or 10") else -> print("number is out of range") }
  26. when when (number) { 1 -> print("number is 1") 2

    -> print("number is 2") 3, 4 -> print("number is 3 or 4") in 5 .. 10 -> print("number is 5, 6, 7, 8, 9, or 10") else -> print("number is out of range") }
  27. when when (number) { 1 -> print("number is 1") 2

    -> print("number is 2") 3, 4 -> print("number is 3 or 4") in 5 .. 10 -> print("number is 5, 6, 7, 8, 9, or 10") else -> print("number is out of range") }
  28. when when (number) { 1 -> print("number is 1") 2

    -> print("number is 2") 3, 4 -> print("number is 3 or 4") in 5 .. 10 -> print("number is 5, 6, 7, 8, 9, or 10") else -> print("number is out of range") }
  29. Extension Functions fun String.toTitleCase(): String { return this.toLowerCase().capitalize() } fun

    String?.toTitleCase(): String? { return this.toLowerCase().capitalize() }
  30. Extension Functions fun String.toTitleCase(): String { return this.toLowerCase().capitalize() } fun

    String?.toTitleCase(): String? { return this.toLowerCase().capitalize() }
  31. Extension Functions fun String.toTitleCase(): String { return this.toLowerCase().capitalize() } fun

    String?.toTitleCase(): String? { return this.toLowerCase().capitalize() }
  32. Default Arguments values public void hello(String name) { if (name

    == null) { name = "World"; } System.out.print("Hello, " + name + "!"); }
  33. Default Arguments values public void hello(String name) { if (name

    == null) { name = "World"; } System.out.print("Hello, " + name + "!"); } fun hello(name: String = "World") { println("Hello, $name!") }
  34. Named parameters fun main(args: Array<String>) { openFile("file.txt", readOnly = true)

    } fun openFile(filename: String, readOnly: Boolean) : File { // custom }
  35. Named parameters fun main(args: Array<String>) { openFile("file.txt", readOnly = true)

    } fun openFile(filename: String, readOnly: Boolean) : File { // custom }
  36. Named parameters fun main(args: Array<String>) { openFile("file.txt", readOnly = true)

    } fun openFile(filename: String, readOnly: Boolean) : File { // custom }
  37. Optional Parameters fun printDetails(name: String, email: String = "", phone:

    String = "NA") { println( /* code */) } fun main(args: Array<String>) { printDetails("Ashish", phone = "999 123", email = "[email protected]") }
  38. Optional Parameters fun printDetails(name: String, email: String = "", phone:

    String = "NA") { println( /* code */) } fun main(args: Array<String>) { printDetails("Ashish", phone = "999 123", email = "[email protected]") printDetails("Ashish", phone = "999 123") }
  39. Lambdas val sum = { x: Int, y: Int ->

    x + y } // type: (Int, Int) -> Int val result = sum(10, 20) numbers.filter({ number -> number.isPrime() }) numbers.filter { number -> number.isPrime() } numbers.filter { it.isPrime() } persons .filter { it.age >= 18 } .sortedBy { it.name } .map { it.email } .forEach { print(it) }
  40. Lambdas val sum = { x: Int, y: Int ->

    x + y } // type: (Int, Int) -> Int val result = sum(10, 20) numbers.filter({ number -> number.isPrime() }) numbers.filter { number -> number.isPrime() } numbers.filter { it.isPrime() } persons .filter { it.age >= 18 } .sortedBy { it.name } .map { it.email } .forEach { print(it) }
  41. Lambdas val sum = { x: Int, y: Int ->

    x + y } // type: (Int, Int) -> Int val result = sum(10, 20) numbers.filter({ number -> number.isPrime() }) numbers.filter { number -> number.isPrime() } numbers.filter { it.isPrime() } persons .filter { it.age >= 20 } .sortedBy { it.name } .map { it.email } .forEach { print(it) }