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

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! 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
  2. Statically typed programming language for the JVM, Android and the

    browser* 3 *Now officially supported by Google for Android development
  3. “Computer languages differ not so much in what they make

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

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

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

    in recent development to native code. ▰ Kotlin comes from Industry ▰ Kotlin is Open Sourced
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. Java public class Document { private static final Document INSTANCE

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

    = new Document(); public static Document getInstance(){ return INSTANCE; } } Kotlin is concise Kotlin object Document { } 17
  14. 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
  15. 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
  16. 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
  17. 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
  18. Extend the functionality of an existing class by adding new

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

    functions to it Extension Functions 23
  20. 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
  21. 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
  22. ▰ 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
  23. “There is no programming language–no matter how structured–that will prevent

    programmers from making bad programs.” - Larry Flon 28 28