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

Demystify Kotlin

Chintan Rathod
September 30, 2017

Demystify Kotlin

Google promoted Kotlin as a first class language on its Java-based Android platform back in May. Since then, the whole development world has been wondering: what is this language? Kotlin is the new lovechild of the JVM developers' world. In this session, we will going to discover the Kotlin language, starting from basics and making our way through the more difficult aspects of it.

Chintan Rathod

September 30, 2017
Tweet

More Decks by Chintan Rathod

Other Decks in Technology

Transcript

  1. Mobile Team Lead Chintan Rathod Syx Automations India Pvt Ltd

    Event Organizer GDG Ahmedabad Follow +chintanrathod16 @chintanrathod16 Blog http://chintanrathod.com
  2. What is Kotlin? Kotlin is statically typed programming language that

    targets the JVM, Android, JavaScript and Native. It's developed by JetBrains. The project started in 2010 and was open source.
  3. Features • Null safety • Short & Sweet • Lamdas

    • Flexible • Mutability protection • Smart casts
  4. Null safety • Explicitly requires to declare null var s

    : String = "Chintan" s = null // compilation error var s : String? = “Rathod" s = null // OK
  5. Short & Sweet • Less number of code • Easy

    methods, functions and declarations view.setOnClickListener { view: View -> //do something }
  6. Flexible • You can use Java class & libraries along

    with Kotlin as interoperability is very much between both languages.
  7. var • Use var for something that will be changed

    • Mutable variables can be assigned multiple times
  8. val • Use val for something won’t changed • Immutable

    variables can be assigned only single time (Constants)
  9. var vs val var mutable : Int = 5 val

    immutable : Int = 5 mutable = 10 // Possible immutable = 10 // Not possible
  10. Android Studio 3.0+ It has in-built support of Kotlin For

    Android Studio 2.x version, you require to install plugin. You can do it by File -> Settings -> Plugin and search for “Kotlin”. Install it and restart your Android Studio.
  11. Syntax Mutable variables var <object_name> : <Type> = <Value> Immutable

    variables val <object_name> : <Type> = <Value>
  12. Declaration var i : Int = 1 var d :

    Double = 1.1 var f : Float = 1.1F var l : Long = 1L var c : Char = 'c' var s : String = "Chintan"
  13. Strings var s = "Chintan" var ss = "My name

    is $s" // My name is Chintan var c = s[2] // this will assign character 'i'
  14. Null safety var s : String = "Chintan" s =

    null // compilation error var s : String? = "Chintan" s = null // OK
  15. Type cast val i : Int = 1 val d

    : Double = i // Not possible val d : Double = i.toDouble() // OK
  16. Binary Expression Function a + b a.plus(b) a - b

    a.minus(b) a * b a.times(b) a / b a.div(b) a % b a.rem(b), a.mod(b) (deprecated) a..b a.rangeTo(b)
  17. Operator Overriding data class Test(val count: Int) { operator fun

    plus(increment: Int): Test { return Test(count + increment) } } var test = Test(10) println(test + 10)
  18. Operator Overriding data class Test(val count: Int) { operator fun

    plus(increment: Int): Test { return Test(count + increment) } } var test = Test(10) println(test + 10) // Output -> Test(count=20)
  19. Operator Overriding data class Test(val count: Int) operator fun Test.unaryMinus()

    = Test(-count) val test = Test(10) println(-test) // Output -> Test(count = -10)
  20. Difference public class Student{ String name; Student(String name){ this.name =

    name; } public String getName(){ return name; } } class Student(val name: String) // That’s it. No more code
  21. Properties class Student( // Read-only, generates field + getter val

    name : String, // Mutable, generates field + getter + setter var age : Int )
  22. Constructor A class in Kotlin can have a primary constructor

    and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters). class Person constructor(firstName: String) { }
  23. Constructor with code The primary constructor cannot contain any code.

    Initialization code can be placed in initializer blocks, which are prefixed with the init keyword: class Customer(name: String) { init { logger.info("Customer initialized with value ${name}") } }
  24. No return // No return type. (can also be -->

    fun sayIt (a: String): Unit) fun sayIt (a: String) { println(a) }
  25. MainActivity.java public class MainActivity extends Activity { @Override protected void

    onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }