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

Kotlin for Beginners

Kotlin for Beginners

Adora Nwodo

August 10, 2019

Transcript

41 slides
  1. Hi, I’m Adora Software Engineer Creator, adorahack.com Co-organizer GDG Ajah,

    Unstack, Android Ngr Twitter: @theadoranwodo YouTube: adorahack
  2. Some Kotlin Features • Null safety • Extension functions •

    Lambdas • Type inference • Smart casts • Data classes • String templating • Coroutines • Raw Strings • Sealed classes • Generics • Singletons
  3. Mutable variables Can be modified after it is created var

    name = "Elizabeth" //some more code here .... name = "Adora" //name is now Adora
  4. Immutable variables Can’t be modified after it is created val

    name = "Elizabeth" //some more code here .... name = "Adora" //ERROR
  5. val myDouble: Double = 2020.0 val myFloat: Float = 222.0f

    val myLong: Long = 903999938L val myInt: Int = 300 val myShort: Short = 15 val myByte: Byte = 2 Numbers
  6. val x: Byte = 3 val y: Int = x

    // THIS DOES NOT COMPILE Invalid Numeric Assignment
  7. Declared using the Boolean keyword. Two values: true or false.

    val isFemale: Boolean = true val isMale: Boolean = false Boolean
  8. Escaped strings may have escaped characters in them. Raw strings

    can contain newlines and arbitrary text. val str = “Welcome to Kotlin Everywhere \n” val text = “”” for (a in “nenne”) Print a “”” Escaped Strings & Raw Strings
  9. Created using a library function arrayOf() Kotlin also has specialized

    classes to represent arrays of primitive types without boxing overhead: ByteArray, ShortArray, IntArray val arr = arrayOf(1,2,3,4,5) //Array with values [1, 2, 3, 4, 5] val intarr: IntArray = intArrayOf(10,20,30,40,50,60,70,80,90,100) Arrays
  10. TYPE INFERENCE val num = 20 //Integer val str =

    “Kotlin” //String val isFemale = true //Boolean val letter = ‘a’ //Character Declare variables without adding DataType
  11. val str: String = “Something about a lazy fox” str

    = “Another String” // This works str = true // Error! Boolean value cannot be in string variable str = 14 // Error! Int value cannot be in string variable
  12. A class is a blueprint that defines attributes and behaviours

    that you use to create objects. A class is declared using the class keyword. class Car{ //class header //class body } Classes
  13. A Kotlin class can have a primary constructor and one

    or more secondary constructors. class Car(brand: String){ //primary constructor in class header init{ //initializer block print("The car brand is $brand") } constructor(color: String, brand: String) : this(brand){ //secondary constructor print("In the secondary constructor of a $color $brand car") } } Constructors
  14. An object is an instance of a class. An object

    has states and behaviours. Objects are created like this: val myCar = Car(“Tesla”) // No new keyword Objects
  15. fun paintOurCar(color: String){ print("Our car is now $color") } Calling

    a function from another class: val myCar = Car("Toyota") myCar.paintOurCar("red") Calling a function from the same class: paintOurCar("red") Functions fun changeCarColor(color: String): String { return "Our car is now $color" }
  16. Defining properties class Car { val name: String = “Adora’s

    car” val brand: String = “Mercedes Benz” } Accessing these properties val myCar = Car() val carname = myCar.name val carbrand = myCar.brand Properties
  17. In Kotlin, a nested class is static by default. class

    Greeting { class NestedGreeting { fun greet(){ print( “Hello, how’s it going?” ) } } } Calling the nested class Greeting.NestedGreeting(). greet() Nested classes
  18. A nested class may be marked as inner to be

    able to access members of the outer class class Greeting { private val welcomeMsg = “Hello, how’s it going? ” inner class NestedGreeting { fun greet(){ print( welcomeMsg) } } } Calling the inner class Greeting().NestedGreeting(). greet() Inner classes
  19. class Tesla : Car { } open class FirstClass {

    open fun play () { print("Yay. Let's play chess.") } } class SecondClass: FirstClass() { fun aFunction() { print("This is a function") } } fun main() { var sampleObject = SecondClass() sampleObject.play() //OUTPUT: Yay. “Let’s play chess } o Inheritance
  20. class Tesla : Car { } open class FirstClass {

    open fun play () { print("Yay. Let's play chess.") } } class SecondClass: FirstClass() { override fun play() { print("Chess is boring. Let's play checkers") } } fun main() { var sampleObject = SecondClass() sampleObject.play() } o Inheritance
  21. class Tesla : Car { } open class FirstClass {

    open fun play () { print("Yay. Let's play chess.") } } class SecondClass: FirstClass() { override fun play() { print("Chess is boring. Let's play checkers") } } fun main() { var sampleObject = SecondClass() sampleObject.play() //OUTPUT: Chess is boring. Let’s play checkers } o Inheritance
  22. data class User(val firstname: String, val lastname: String, val username:

    String, val email: String) The equals(), hashCode(), toString(), copy() and componentN() functions are added automatically. Using the “copy” function val olduser = User("Nenne", "Nwodo", "adoranwodo", "[email protected]") val newuser = olduser.copy(firstname = "Adora") Kotlin
  23. Rules for using data classes • The primary constructor needs

    to have at least one parameter e.g. data class User(val firstname: String) • All primary constructor parameters need to be marked as val or var • Data classes cannot be abstract, open, sealed or inner;
  24. Create and instantiate a data class val user = User("Nenne",

    "Nwodo", "adoranwodo", "[email protected]") val firstname = user.firstname val lastname = user.lastname val username = user.username val email = user.email print("$firstname $lastname is user $username and their email is $email")
  25. Destructuring declarations val user = User("Nenne", "Nwodo", "adoranwodo", "[email protected]") val

    (firstname, lastname, username,email) = user print("$firstname $lastname is user $username and their email is $email")
  26. What we covered • Some Kotlin Features • Variable declarations

    • Data types • Classes & Objects • Inheritance • Data classes

More Decks by Adora Nwodo

Other Decks in Programming