Share
Embed
Transcript
41 slides
-
Hi, I’m Adora Software Engineer Creator, adorahack.com Co-organizer GDG Ajah,
Unstack, Android Ngr Twitter: @theadoranwodo YouTube: adorahack -
Some Kotlin Features • Null safety • Extension functions •
Lambdas • Type inference • Smart casts • Data classes • String templating • Coroutines • Raw Strings • Sealed classes • Generics • Singletons -
Mutable variables Can be modified after it is created var
name = "Elizabeth" //some more code here .... name = "Adora" //name is now Adora -
Immutable variables Can’t be modified after it is created val
name = "Elizabeth" //some more code here .... name = "Adora" //ERROR -
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 -
val x: Byte = 3 val y: Int = x
// THIS DOES NOT COMPILE Invalid Numeric Assignment -
Declared using the Char keyword. Represented in single quotes. val
firstLetter: Char = ‘n’ Characters -
Declared using the Boolean keyword. Two values: true or false.
val isFemale: Boolean = true val isMale: Boolean = false Boolean -
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 -
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 -
TYPE INFERENCE val num = 20 //Integer val str =
“Kotlin” //String val isFemale = true //Boolean val letter = ‘a’ //Character Declare variables without adding DataType -
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 -
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 -
• Constructors • Functions • Properties • Nested classes •
Inner classes Class members -
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 -
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 -
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" } -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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; -
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") -
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") -
What we covered • Some Kotlin Features • Variable declarations
• Data types • Classes & Objects • Inheritance • Data classes -
Other resources • https://kotlinlang.org/docs/reference/ • https://antonioleiva.com/ • https://adorahack.com/category/kotlin/zero-to-kotlin-hero • https://blog.teamtreehouse.com/absolute-beginners-guide-kotlin
• https://www.udemy.com/kotlin-course/ • https://play.kotlinlang.org/byExample/