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

Kotlin for beginners

Kotlin for beginners

Ziem

May 15, 2023
Tweet

More Decks by Ziem

Other Decks in Programming

Transcript

  1. Introduction • Kotlin was created by JetBrains. • It was

    fi rst released in 2011. • It is now an o ffi cial language for Android development.
  2. Introduction • Kotlin was created by JetBrains. • It was

    fi rst released in 2011. • It is now an o ffi cial language for Android development.
  3. Introduction • Kotlin was created by JetBrains. • It was

    fi rst released in 2011. • It is now an o ffi cial language for Android development.
  4. Introduction • Kotlin was created by JetBrains. • It was

    fi rst released in 2011. • It is now an o ffi cial language for Android development.
  5. 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); } }
  6. 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); } }
  7. 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); } }
  8. ;

  9. Variables val name: String = "John" var isFun: Boolean =

    true val age: Int = 25 mutable immutable
  10. Variables name val name: String = "John" var isFun: Boolean

    = true val age: Int = 25 mutable immutable
  11. Variables name type val name: String = "John" var isFun:

    Boolean = true val age: Int = 25 mutable immutable
  12. Variables name type value val name: String = "John" var

    isFun: Boolean = true val age: Int = 25 mutable immutable
  13. Variables name type value val name = "John" var isFun

    = true val age = 25 mutable immutable
  14. var (mutable) var value: Int = 123 value = 1

    value = 332 println(value) // 332
  15. val (immutable) val workshop: String = "Kotlin for beginners" workshop

    = "JS for beginners" val cannot be reassigned
  16. Functions fun sum(a: Int, b: Int): Int { return a

    + b } name arguments return type
  17. Functions fun sum(a: Int, b: Int) = a + b

    name arguments return type
  18. Functions fun hello(name: String, surname: String): Unit { println("Hello $name

    $surname") } hello("Bill", "Gates") / / Hello Bill Gates!
  19. Functions fun hello(name: String, surname: String): Unit { println("Hello $name

    $surname") } hello("Bill", "Gates") / / Hello Bill Gates!
  20. Named arguments hello(name = "Gates", surname = "Bill") // Hello

    Gates Bill! hello(surname = "Bill", name = "Gates") // Hello Gates Bill!
  21. 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") }
  22. 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") }
  23. 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") }
  24. Control fl ows val age = 18 when { age

    > = 100 -> println("Really old adult") age > = 18 -> println("An adult") else -> println("Not an adult") }
  25. 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") }
  26. 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)
  27. Loops for (i in 0 . . 50) { //

    0 <= i <= 50 println(i) }
  28. 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".
  29. Null safety val age: Int = null val name: String?

    = null val length = name.length if (name != null) { val length = name.length }
  30. val age: Int = null val name: String? = null

    val length = name.length if (name != null) { val length = name.length } Null safety
  31. val age: Int = null val name: String? = null

    val length = name.length if (name != null) { val length = name.length } Null safety
  32. val age: Int = null val name: String? = null

    val length = name.length if (name != null) { val length = name.length } Null safety
  33. val age: Int = null val name: String? = null

    val length = name.length if (name != null) { val length = name.length } Null safety
  34. val age: Int = null val name: String? = null

    val length = name.length if (name != null) { val length = name.length } Null safety
  35. 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 ""
  36. 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 ""
  37. 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 ""
  38. 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 ""
  39. 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
  40. Classes • Closed by default • Public by default class

    Color( val red: Int, val green: Int, val blue: Int, val alpha: Int, )
  41. Interfaces class Color( .. . ) : Printable { override

    fun print() { val hex = String.format("#%02x%02x%02x", red, green, blue) println(hex) } }
  42. Special classes • Abstract classes • Data classes • Enum

    classes • Inline classes • Sealed classes • Object
  43. Data classes • data keyword • toString() implementation • equals()

    / hashcode() implementation • copy() implementation • …
  44. Data classes data class Color( val red: Int, val green:

    Int, val blue: Int, val alpha: Int, )
  45. Data classes class Color( val red: Int, ... ) val

    red = Color(255, 0, 0, 255) println(red) // Color@5679c6c6
  46. 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)
  47. Collections val cipher = arrayOf(50, 51, 89, ... ) val

    authors = listOf("Bjørn", "Ingrid") val authors = mutableListOf("Bjørn", "Ingrid")
  48. 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")
  49. 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"]
  50. 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) }