$30 off During Our Annual Pro Sale. View Details »

Hello 'Kotlin' World

Hello 'Kotlin' World

Kotlin programming language is gaining popularity nowadays. It’s a modern language that gives more power in everyday routines.It’s a pragmatic language with a very low learning curve and can be quickly grasped by Java developers.

Talk was given at Code & Coffee YVR in Vancouver
Video Link: https://t.co/05Fz6Rg823

Nishant Srivastava

May 18, 2017
Tweet

More Decks by Nishant Srivastava

Other Decks in Technology

Transcript

  1. Hello ‘ ’ World! Nishant Srivastava @nisrulz

  2. HELLO! I am Nishant Srivastava Software Engg./Founding Team member (at)

    Omni Labs, Inc. Lead Organizer (at) GDG New Delhi You can find me at @nisrulz 2
  3. Statically typed programming language for the JVM, Android and the

    browser* 3 *Now officially supported by Google for Android development
  4. 4 WHY ? ? ?

  5. Just-Another-Language for JVM? Java, Groovy, Scala, Clojure, Ceylon, Fantom, Xtend,

    Kotlin ? 5 WHY ? ? ?
  6. “Computer languages differ not so much in what they make

    possible, but in what they make easy." - Larry Wall 6 6
  7. Kotlin 7 ▰ Kotlin compiles to JVM bytecode, Javascript and

    in recent development to native code.
  8. Kotlin 8 ▰ Kotlin compiles to JVM bytecode, Javascript and

    in recent development to native code. ▰ Kotlin comes from Industry
  9. Kotlin 9 ▰ Kotlin compiles to JVM bytecode, Javascript and

    in recent development to native code. ▰ Kotlin comes from Industry ▰ Kotlin is Open Sourced
  10. Kotlin 10 ▰ Kotlin compiles to JVM bytecode, Javascript and

    in recent development to native code. ▰ Kotlin comes from Industry ▰ Kotlin is Open Sourced ▰ Kotlin is 100% Interoperable with Java
  11. Everything awesome in Kotlin How does it fair when actually

    used in code? 11 Features
  12. Java final class User { private String name; public User(String

    name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } Kotlin is concise 12
  13. Java final class User { private String name; public User(String

    name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } Kotlin is concise Kotlin class User(var name: String) 13
  14. Java final int x = 0 // value; final String

    xResult; switch (x){ case 0: case 11: xResult = "0 or 11"; break; case 1: case 2: //... case 10: xResult = "from 1 to 10"; break; xResult = "otherwise"; } Kotlin is concise 14
  15. Java final int x = 0 // value; final String

    xResult; switch (x){ case 0: case 11: xResult = "0 or 11"; break; case 1: case 2: //... case 10: xResult = "from 1 to 10"; break; xResult = "otherwise"; } Kotlin is concise Kotlin val x = 0 // value val xResult = when (x) { 0, 11 -> "0 or 11" in 1..10 -> "from 1 to 10" else -> "otherwise" } 15
  16. Java public class Document { private static final Document INSTANCE

    = new Document(); public static Document getInstance(){ return INSTANCE; } } Kotlin is concise 16
  17. Java public class Document { private static final Document INSTANCE

    = new Document(); public static Document getInstance(){ return INSTANCE; } } Kotlin is concise Kotlin object Document { } 17
  18. Java // type declared explicitly final String a = “abc”

    final int b = 4; final double c = 0.7 final List<String> d = new ArrayList<>() Kotlin has auto Type Inference 18
  19. Java // type declared explicitly final String a = “abc”

    final int b = 4; final double c = 0.7 final List<String> d = new ArrayList<>() Kotlin has Auto Type Inference Kotlin val a = "abc" // type inferred to String val b = 4 // type inferred to Int // type declared explicitly val c: Double = 0.7 val d: List<String> = ArrayList() 19
  20. Java final String name = null; String lastName; lastName =

    null; // Code compiles lastName.concat("."); // NPE // Solution to avoid NPE if(lastName!=null){ lastName.concat("."); } Kotlin has Null Safety inbuilt 20
  21. Java final String name = null; String lastName; lastName =

    null; // Code compiles lastName.concat("."); // NPE // Solution to avoid NPE if(lastName!=null){ lastName.concat("."); } Kotlin has Null Safety inbuilt Kotlin val name : String? = null var lastName : String? lastName = null var firstName : String firstName = null // Compilation error!! 21
  22. Extend the functionality of an existing class by adding new

    functions to it Extension Functions 22
  23. Extend the functionality of an existing class by adding new

    functions to it Extension Functions 23
  24. Extend the functionality of an existing class by adding new

    functions to it class Car { var brandName = ... var modelName = ... } // Maybe the author of the Car class forgot to include this fun Car.setName(brandName: String, modelName: String) { this.brandName = brandName this.modelName = modelName } Extension Functions 24
  25. Extend the functionality of an existing class by adding new

    functions to it // Call the function directly on the instance object val car= Car() car.setName(“Audi”, “Q7”) Extension Functions 25
  26. ▰ Generates code compatible from Java 6 and above ▰

    Functional programming support with zero-overhead lambdas and inline functions ▰ Markdown for documentation ▰ The == operator checks for structural equality, no need of calling equals() ▰ String interpolation “works like ${this.example}!” ▰ Coroutines i.e async/await make async programming bearable (Kotlin 1.1) ▰ Smart casts ▰ Default arguments ▰ Named arguments ... But there is more... 26
  27. References ▰ https://kotlin.link/ ▰ https://github.com/Kotlin ▰ https://learnxinyminutes.com/docs/kotlin/ ▰ https://kotlinlang.org/ ▰

    https://fabiomsr.github.io/from-java-to-kotlin 27
  28. “There is no programming language–no matter how structured–that will prevent

    programmers from making bad programs.” - Larry Flon 28 28
  29. 29 THANKS! Any questions? You can find me at @nisrulz

    & nisrulz@gmail.com