Slide 1

Slide 1 text

Hello ‘ ’ World! Nishant Srivastava @nisrulz

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Statically typed programming language for the JVM, Android and the browser* 3 *Now officially supported by Google for Android development

Slide 4

Slide 4 text

4 WHY ? ? ?

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

“Computer languages differ not so much in what they make possible, but in what they make easy." - Larry Wall 6 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Everything awesome in Kotlin How does it fair when actually used in code? 11 Features

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Java // type declared explicitly final String a = “abc” final int b = 4; final double c = 0.7 final List 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 = ArrayList() 19

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

▰ 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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

“There is no programming language–no matter how structured–that will prevent programmers from making bad programs.” - Larry Flon 28 28

Slide 29

Slide 29 text

29 THANKS! Any questions? You can find me at @nisrulz & [email protected]