Slide 1

Slide 1 text

Kotlin Everywhere!

Slide 2

Slide 2 text

Akshay Chordiya ● Founder of BitFurther ● Android Developer by heart ● Active community speaker ● Co-author of upcoming book “Kotlin Blueprints” ● Graduated in 2016 @Aky @Akshay_Chordiya AkshayChordiya

Slide 3

Slide 3 text

Hardik Trivedi ● A Computer program writer ● Works at Globant ● Love writing tech blogs and contributing back to community ● Mentor college graduates and professional who wants to go for mobile app development ● Co-author of an upcoming book “Kotlin Blueprints” https://trivedihardik.wordpress.com/ hardik-trivedi

Slide 4

Slide 4 text

Agenda ● What’s Kotlin? ● Why Kotlin? ● Syntax ● Future of Kotlin ● Platform walkthrough ● Conclusion ● Q&A

Slide 5

Slide 5 text

What’s Kotlin?

Slide 6

Slide 6 text

“Kotlin is a new programming language targeting JVM, Android and even JavaScript.”

Slide 7

Slide 7 text

Kotlin ● Modern & Statically typed ● Targets JVM, Android, JavaScript and Native* ● Works everywhere where Java works ● 100% interoperable with Java™ ● Combines Object Oriented and Functional features ● Made by good folks at JetBrains ● Open source * denotes it’s still in preview

Slide 8

Slide 8 text

Big Players using Kotlin Logos belong to respective company

Slide 9

Slide 9 text

Kotlin Philosophy

Slide 10

Slide 10 text

Kotlin Philosophy Concise Safe Interoperable Tooling Image credits - kotlinlang.org

Slide 11

Slide 11 text

Kotlin Philosophy Concise Interoperable Tooling Image credits - kotlinlang.org Safe

Slide 12

Slide 12 text

Kotlin Philosophy Concise Tooling Image credits - kotlinlang.org Safe Interoperable

Slide 13

Slide 13 text

Kotlin Philosophy Concise Image credits - kotlinlang.org Safe Interoperable Tooling

Slide 14

Slide 14 text

Kotlin Philosophy Concise Safe Interoperable Tooling Image credits - kotlinlang.org

Slide 15

Slide 15 text

Why Kotlin?

Slide 16

Slide 16 text

Why Kotlin ● It's a fun language! ● Tired of Java….JS ● Helps you be more productive ● Everywhere ● Push from Google and Android community ● Cool language features ● Functional nature

Slide 17

Slide 17 text

Pillars of Functional Programming ● Explicitness  ● Purity ● Immutability ● Concurrency  ● Higher Order Functions

Slide 18

Slide 18 text

Syntax Crash course

Slide 19

Slide 19 text

Variables // String (Implicitly inferred) val spell = "Lumos" // Int val number = 74 // Explicit type var spells: List = ArrayList()

Slide 20

Slide 20 text

“Use val for a value that won’t change” - Immutable val vs var “Use var for something that will vary with time.” - Mutable

Slide 21

Slide 21 text

fun add(a: Int, b: Int): Int { return a + b } Functions fun add(a: Int, b: Int) = a + b fun add(a: Int = 0, b: Int = 0) = a + b

Slide 22

Slide 22 text

Features

Slide 23

Slide 23 text

Null Safety

Slide 24

Slide 24 text

// Compile time error val spell: String = null // OK val spell: String? = null Declaring null variables

Slide 25

Slide 25 text

// Compile time error var spell: String = "Lumos" spell = null // OK var spell: String? = "Lumos" spell = null Even...

Slide 26

Slide 26 text

// Safe val spell: String? = null val length = spell?.length // Safe with else val length = spell?.length ?: -1 Kotlin-ized way

Slide 27

Slide 27 text

Data Class

Slide 28

Slide 28 text

User class - Java public class User { private String firstName; private String lastName; public User() { this.firstName = ""; this.lastName = ""; } // Getter - Setter, hashCode, equals(), and other stuff @Override public String toString() { return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}'; } } ~60 lines

Slide 29

Slide 29 text

User class - Kotlin data class User(var firstName = "", var lastName = "") That’s it!

Slide 30

Slide 30 text

Extension function

Slide 31

Slide 31 text

fun String.isEmail(): Boolean { // this -> value of string return Patterns.EMAIL_ADDRESS.matcher(this).matches() } Extension Function

Slide 32

Slide 32 text

Usage: Extension Function "[email protected]".isEmail() if("[email protected]".isEmail()) { // It's valid email address }

Slide 33

Slide 33 text

Smart Cast

Slide 34

Slide 34 text

Smart Cast // Java way if (text instanceof String) { int length = ((String) text).length(); } // Kotlin way if (text is String) { // Smart casting val length = text.length }

Slide 35

Slide 35 text

String interpolation

Slide 36

Slide 36 text

String interpolation fun sayHello(message: String) { println("Welcome $message") } fun sayTime() { println("Time: ${System.currentTimeMillis()}") }

Slide 37

Slide 37 text

Future of Kotlin Image credits - Freepik

Slide 38

Slide 38 text

The idea JVM Native iOS, etc. JS Kotlin Android *Native still in preview Spring, etc.

Slide 39

Slide 39 text

The idea ● Android - Web - iOS teams will be able to work together, design together and solve problems together ● Enable Code sharing ● Easier development cycle with less hassle of maintaining multiple teams for each platform ● Easy to incorporate in your existing project. Thanks to the interoperability

Slide 40

Slide 40 text

Kotlin For Android

Slide 41

Slide 41 text

Kotlin for Android ● Jetbrains and Google, very much focused for Android. ● It’s becoming more and more popular. ● Philosophy and Spirit of Kotlin and Android are matching. ● Android is enabled with excellent tooling support from JetBrains and Google. ● Kotlin is perfect whose project still compiles on Java 6 and want to have fun with lucrative feature of Java 8 ● It has 100% interoperability with Java from day one. One can start using Kotlin in Java project on any moment of the day. ● It’s a mocktail with power of OOP language and beauty of functional programming language.

Slide 42

Slide 42 text

Kotlin for Android ● Android is smart and language is smarter ● Kotlin has so much to offer for Android, it’s concise, expressive and powerful. ● With the Null Safety Kotlin actually eliminates the possibility of app crash. ● It’s concise, so it’s less line of code and less verbosity around the business logic. ● Co routines and delegates reduces great amount of code ● Anko is dedicated library from JetBrains which solves all Android problem ● Android loves ceremonies of API ● Lint also supports Kotlin.

Slide 43

Slide 43 text

Kotlin for Android // Java Button myButton = findViewById(R.id.my_button); myButton.setText("Click"); // Kotlin import kotlinx.android.synthetic.main.activity_home.* myButton.text="Click"

Slide 44

Slide 44 text

// Java Editor editor = sharedpreferences.edit(); editor.putString("key", "value"); editor.apply(); Kotlin for Android // Kotlin private var isConnectedToTwitter: Boolean by DelegatedPreference(this, IS_CONNECTED_TO_TWITTER, false)

Slide 45

Slide 45 text

● No support for statics ● Extra runtime overhead ● Adds around 6K method count ● Increases the app size ● Mocking is the problem Kotlin for Android

Slide 46

Slide 46 text

Kotlin For JS

Slide 47

Slide 47 text

Kotlin for JavaScript ● Do not think support for JavaScript is on experimental basis. ● All Koltin features are fully supported ● Multiple ways to compile Koltin to Javascript ● Koltin’s package for JavaScript includes libraries which supports DOM element manipulation, and also Graphical elements using WebGL

Slide 48

Slide 48 text

Kotlin for JavaScript ● Let’s understand how Koltin code gets converted in JavaScript import kotlin.browser.document fun main(args: Array) { document.bgColor="FF0000" val message = "Hello JavaScript!" println(message) }

Slide 49

Slide 49 text

Kotlin for JavaScript ● Index.html will look like Console Output

Slide 50

Slide 50 text

if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'KotlinWeb'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'KotlinWeb'."); } var KotlinWeb = function (_, Kotlin) { 'use strict'; var println = Kotlin.kotlin.io.println_s8jyv4$; function main(args) { document.bgColor = 'FF0000'; var message = 'Hello JavaScript!'; println(message); } _.main_kand9s$ = main; Kotlin.defineModule('KotlinWeb', _); main([]); return _; }(typeof KotlinWeb === 'undefined' ? {} : KotlinWeb, kotlin);

Slide 51

Slide 51 text

● Koltinx.html is a library which has DSL (Domain Specific Language) to write HTML ● You can build a DOM tree window.setInterval({ val myDiv = document.create.div("panel") { p { +"Here is " a("http://kotlinlang.org") { +"official Kotlin site" } } } document.getElementById("container")!!.appendChild(myDiv) document.getElementById("container")!!.append { div { +"added it" } } }, 1000L)

Slide 52

Slide 52 text

● You can build HTML directly to System.out.appendHTML().html { body { div { a("http://kotlinlang.org") { target = ATarget.blank +"Main site" } } } }

Slide 53

Slide 53 text

Kotlin Native

Slide 54

Slide 54 text

Going native ● Kotlin without a VM! ● Compiles down to machine code ● Self-contained executable for iOS, macOS, Linux and Windows ● It uses LLVM compiler ● It’s still in preview! Not in a shippable state at all ● Interops with C ● It will be useful for building apps for iOS and embedded systems

Slide 55

Slide 55 text

Conclusion

Slide 56

Slide 56 text

“With Kotlin our code base is shrinked down.”

Slide 57

Slide 57 text

“Plenty of app crashes are cut down”

Slide 58

Slide 58 text

“Kotlin makes development more fun again”

Slide 59

Slide 59 text

Further Resources ● All the images and logos used are trademarks of respective companies. ● Kotlin Official Website ● Kotlin and Android ● Getting started with Kotlin - Video ● Sample News App with 100% Kotlin ● Why Kotlin-ize your Android Development Medium - Akshay Chordiya ● Kotlin - Goodbye findViewById ● Easter Functions in Kotlin ● Being more productive with Kotlin

Slide 60

Slide 60 text

Thank you! Slides: https://goo.gl/eTXGyL