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

Kotlin 101

Kotlin 101

The Basics of Kotlin Programming Language

Hussain Mukadam

October 14, 2017
Tweet

More Decks by Hussain Mukadam

Other Decks in Programming

Transcript

  1. Basic Types and Syntax • Classes and fields • Primary

    constructor • Function and overrides • Mutability & Nullability • Equality • Any versus object • Imports and Data Classes
  2. Basic Syntax fun main(args: Array<String>) { println("Hello Kotlin!") } public

    static void main(String[] args) { System.out.println(“Good bye Java!”); }
  3. Basic Syntax val - Immutable (For values that don’t change)

    var - Mutable (For variables that change) //Variable type is inferred from the context val name = "John" name = 2 //Error, because name type is String //Explicit type var list : List<String> = ArrayList()
  4. class Person open class Person private class Person class Person(val

    name: String, val age: Int) class Person(var name: String, var age: Int) class Person private constructor(name: String, age: Int) annotation class Inject class Person @Inject constructor(val name: String, val age: Int) Class Declarations
  5. class Person(val name: String, val age: Int) fun main(args: Array<String>)

    { val person = Person("John Doe", 30) //Reassigning person.age = 40 //Error } //Creates a setter class Person(val name: String, var age: Int) Fields
  6. class Person(val name: String, var age: Int) fun main(args: Array<String>)

    { val person = Person("John Doe", 30) //Assigning null person.age = null //Error } //Makes it a Nullable type class Person(val name: String, var age: Int?) Nullability
  7. //Do this, when you’re sure enough it won’t be null

    val nextAge = person.age!! + 1 //Use Elvis Operator val nextAge = (person.age ?: 0) + 1 //Error val nextAge = person.age + 1 //We can use safe call operator to safely call methods on potentially nullable variables savedInstanceState?.doSomething()
  8. class Person(val name: String, var age: Int){ override fun toString():

    String { return "$name is $age years old" } override fun equals(other: Any?): Boolean { return (other is Person && other.name == name) } } Equality and String interpolation
  9. Aliases and Importing functions import com.example.MyClass import com.example.* //Import with

    an Alias import com.example.MyLongClassName as MLCN //Import Package level function import com.example.hello
  10. Default Imports • kotlin.* • kotlin.annotation.* • kotlin.collections.* • kotlin.comparisons.*

    • kotlin.io.* • kotlin.ranges.* • kotlin.sequences.* • kotlin.text.* • java.lang • kotlin.jvm.*
  11. package com.example.pkg1 class Greeter{ fun hello() = "Hello from Package

    1" } package com.example.pkg2 class Greeter{ fun hello() = "Hello from Package 2" } Expression body
  12. package com.example.pkg3 import com.example.pkg1.Greeter as G1 import com.example.pkg2.Greeter as G2

    fun main(args: Array<String>) { println(G1().hello()) println(G2().hello()) } Aliases
  13. Data Classes • data class Person(val name: String, val age:

    Int) • Along with this you get - • equals/hashcode • toString() like Person(name=John Doe, age=42) • Destructuring declarations - componentN() • Copy with modified attributes - copy()
  14. data class Person(val name: String, val age: Int) fun main(args:

    Array<String>) { val person = Person("John Doe", 42) val (name, age) = person } Destructuring Declaration Copy() val person2 = person.copy("Johnny Depp", 54) println(person2)
  15. Next in the series - • Singleton (object keyword) •

    Enabling Inheritance • Object Expressions • Companion Objects • Efficient Type Checking and Smart Casts • Extension functions, Higher order functions, etc
  16. • Kotlin Lang on Slack • Talking Kotlin Podcast by

    Hadi Hariri • Learning Kotlin Series on Fragmented Podcast • From Java to Kotlin • Antonio Leiva’s blog • Android development with Kotlin - Jake Wharton • Kotlinize your Android Development - Akshay Chordiya References