Share
Embed
Kotlin for beginners
Ziem
May 15, 2023
Transcript
97 slides
-
Introduction • Kotlin was created by JetBrains. • It was
fi rst released in 2011. • It is now an o ffi cial language for Android development. -
Introduction • Kotlin was created by JetBrains. • It was
fi rst released in 2011. • It is now an o ffi cial language for Android development. -
Introduction • Kotlin was created by JetBrains. • It was
fi rst released in 2011. • It is now an o ffi cial language for Android development. -
Introduction • Kotlin was created by JetBrains. • It was
fi rst released in 2011. • It is now an o ffi cial language for Android development. -
Pros • Concise and readable code. • Built-in null safety.
• Interoperability with Java. -
Pros • Concise and readable code. • Built-in null safety.
• Interoperability with Java. -
Pros • Concise and readable code. • Built-in null safety.
• Interoperability with Java. -
Pros • Concise and readable code. • Built-in null safety.
• Interoperability with Java. -
Kotlin vs Java fun main(args: Array<String>) { val numbers =
listOf(1, 2, 3, 4, 5) val evenNumbers = numbers.filter { it % 2 = = 0 } println(evenNumbers) } import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList <> (); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); Predicate<Integer> isEven = n - > n % 2 == 0; List<Integer> evenNumbers = new ArrayList <> (); for (int number : numbers) { if (isEven.test(number)) { evenNumbers.add(number); } } System.out.println(evenNumbers); } } -
Kotlin vs Java fun main(args: Array<String>) { val numbers =
listOf(1, 2, 3, 4, 5) val evenNumbers = numbers.filter { it % 2 = = 0 } println(evenNumbers) } import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList <> (); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); Predicate<Integer> isEven = n - > n % 2 == 0; List<Integer> evenNumbers = new ArrayList <> (); for (int number : numbers) { if (isEven.test(number)) { evenNumbers.add(number); } } System.out.println(evenNumbers); } } -
Kotlin vs Java fun main(args: Array<String>) { val numbers =
listOf(1, 2, 3, 4, 5) val evenNumbers = numbers.filter { it % 2 = = 0 } println(evenNumbers) } import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList <> (); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); Predicate<Integer> isEven = n - > n % 2 == 0; List<Integer> evenNumbers = new ArrayList <> (); for (int number : numbers) { if (isEven.test(number)) { evenNumbers.add(number); } } System.out.println(evenNumbers); } } -
UByte ULong UInt UShort Types Int Byte Short Long Float
Double Char String -
UByte ULong UInt UShort Types Int Byte Short Long Float
Double Boolean Char String -
UByte ULong UInt UShort Types Int Byte Short Long Float
Double Boolean Char String Array -
Variables val name: String = "John" var isFun: Boolean =
true val age: Int = 25 -
Variables val name: String = "John" var isFun: Boolean =
true val age: Int = 25 mutable immutable -
Variables name val name: String = "John" var isFun: Boolean
= true val age: Int = 25 mutable immutable -
Variables name type val name: String = "John" var isFun:
Boolean = true val age: Int = 25 mutable immutable -
Variables name type value val name: String = "John" var
isFun: Boolean = true val age: Int = 25 mutable immutable -
Variables name type value val name = "John" var isFun
= true val age = 25 mutable immutable -
var (mutable) var value: Int = 123 value = 1
value = 332 println(value) // 332 -
val (immutable) val workshop: String = "Kotlin for beginners" workshop
= "JS for beginners" -
val (immutable) val workshop: String = "Kotlin for beginners" workshop
= "JS for beginners" val cannot be reassigned -
Operators == ! && || = == == != >
< = > = < ! -
Operators == ! && || = == == != >
< = > = < ! is as -
Functions fun sum(a: Int, b: Int): Int { return a
+ b } name arguments -
Functions fun sum(a: Int, b: Int): Int { return a
+ b } name arguments return type -
Functions fun sum(a: Int, b: Int) = a + b
name arguments return type -
Functions fun hello(name: String, surname: String): Unit { println("Hello $name
$surname") } hello("Bill", "Gates") / / Hello Bill Gates! -
Functions fun hello(name: String, surname: String): Unit { println("Hello $name
$surname") } hello("Bill", "Gates") / / Hello Bill Gates! -
Named arguments hello("Bill", "Gates") // Hello Bill Gates! hello("Gates", "Bill")
// Hello Gates Bill! -
Named arguments hello(name = "Gates", surname = "Bill") // Hello
Gates Bill! hello(surname = "Bill", name = "Gates") // Hello Gates Bill! -
Control fl ows val age = 18 if (age >
= 100) { println("This person is really old") } else if (age >= 18) { println("This person is an adult") } else { println("This person is not an adult") } -
Control fl ows val age = 18 if (age >
= 100) { println("This person is really old") } else if (age >= 18) { println("This person is an adult") } else { println("This person is not really old") } -
Control fl ows val age = 18 if (age >
= 100) { println("This person is really old") } else if (age >= 18) { println("This person is an adult") } else { println("This person is not an adult") } -
Control fl ows val age = 18 when { age
> = 100 -> println("Really old adult") age > = 18 -> println("An adult") else -> println("Not an adult") } -
Control fl ows val age = 18 when (age) {
in 0 . . 17 -> println("Not an adult") in 18 .. 99 -> println("An adult") else -> println("Really old adult") } -
Control fl ows val age = 18 val adultState =
when (age) { in 0 . . 17 -> "Not an adult" in 18 .. 99 -> "An adult" else -> "Really old adult" } println(adultState) -
Loops for (i in 0 . . 50) { //
0 <= i <= 50 println(i) } -
Loops var i = 0 while (i <= 50) {
println(i) i ++ } -
Write a program that prints the numbers from 1 to
100. But for multiples of three print "Fizz" instead of the number and for the multiples of fi ve print "Buzz". For numbers which are multiples of both three and fi ve print "FizzBuzz". -
Null safety val age: Int = null val name: String?
= null val length = name.length if (name != null) { val length = name.length } -
val age: Int = null val name: String? = null
val length = name.length if (name != null) { val length = name.length } Null safety -
val age: Int = null val name: String? = null
val length = name.length if (name != null) { val length = name.length } Null safety -
val age: Int = null val name: String? = null
val length = name.length if (name != null) { val length = name.length } Null safety -
val age: Int = null val name: String? = null
val length = name.length if (name != null) { val length = name.length } Null safety -
val age: Int = null val name: String? = null
val length = name.length if (name != null) { val length = name.length } Null safety -
Null safety val name: String? = JavaLib.getName() val length =
name !! .length // throws NullPointerException val length = name ?. length / / returns null val length: String? = name ? . length ?: "" // returns "" -
Null safety val name: String? = JavaLib.getName() val length =
name !! .length // throws NullPointerException val length = name ?. length / / returns null val length: String? = name ? . length ?: "" // returns "" -
Null safety val name: String? = JavaLib.getName() val length =
name !! .length // throws NullPointerException val length = name ?. length / / returns null val length: String? = name ? . length ?: "" // returns "" -
Null safety val name: String? = JavaLib.getName() val length =
name !! .length // throws NullPointerException val length = name ?. length / / returns null val length: String? = name ? . length ?: "" // returns "" -
Null safety val name: String? = JavaLib.getName() val length =
name !! .length // throws NullPointerException val length: Int? = name ? . length // returns null val length: Int = name ?. length ? : 0 // returns 0 -
Classes class Color( val red: Int, val green: Int, val
blue: Int, val alpha: Int, ) -
Classes • Closed by default • Public by default class
Color( val red: Int, val green: Int, val blue: Int, val alpha: Int, ) -
Inheritance open class Event class MenuItemSectionClickEvent(val sectionName: String) : Event()
class AccountItemClickEvent(val name: String) : Event() -
Interfaces class Color( .. . ) : Printable { override
fun print() { val hex = String.format("#%02x%02x%02x", red, green, blue) println(hex) } } -
Special classes • Abstract classes • Data classes • Enum
classes • Inline classes • Sealed classes • Object -
Data classes • data keyword • toString() implementation • equals()
/ hashcode() implementation • copy() implementation • … -
Data classes data class Color( val red: Int, val green:
Int, val blue: Int, val alpha: Int, ) -
Data classes class Color( val red: Int, ... ) val
red = Color(255, 0, 0, 255) println(red) // Color@5679c6c6 -
Data classes data class Color( val red: Int, ... )
val red = Color(255, 0, 0, 255) println(red) // Color(red=255, green=0, blue=0, alpha=255) -
Enum classes enum class TextAlign { LEFT, RIGHT, CENTER, JUSTIFY,
} TextAlign.CENTER -
Collections val cipher = arrayOf(50, 51, 89, ... ) val
authors = listOf("Bjørn", "Ingrid") val authors = mutableListOf("Bjørn", "Ingrid") -
Collections val cipher: Array<Int> = arrayOf(50, 51, 89, .. .
) val authors: List<String> = listOf("Bjørn", "Ingrid") val authors: MutableList<String> = mutableListOf("Bjørn", "Ingrid") -
Collections val mutableAuthors = mutableListOf("Bjørn", "Ingrid") mutableAuthors[1] / / Ingrid
mutableAuthors.size // 2 mutableAuthors.add("Stein") / / ["Bjørn", "Ingrid", "Stein"] mutableAuthors.removeAt(1) // ["Bjørn", "Stein"] mutableAuthors.remove("Stein") / / ["Bjørn"] -
Collection operations data class Student( val name: String, val age:
Int, val subjects: List<String>, ) val students = listOf( Student("Alice", 18, listOf("Math", "Computer Science")), ... ) val selectedStudents = students.filter { it.age == 18 } .filter { "Computer Science" in it.subjects } .map { it.name } .onEach { println(it) }