Document Details

AdoredSanAntonio

Uploaded by AdoredSanAntonio

null

2022

Nora Alhelal

Tags

kotlin programming android development programming language computer science

Summary

This document is a presentation on Kotlin basics. It guides users through fundamental Kotlin programming concepts. The presentation includes examples, explanations and diagrams, suitable for programming enthusiasts or students.

Full Transcript

Android App Development 1. Kotlin Base 2 Kotlin for Android Android mobile development has been Kotlin-first since Google I/O in 2019. Over 50% of professional Android developers use Kotlin as their primary language, while only 30% use Java as their main language....

Android App Development 1. Kotlin Base 2 Kotlin for Android Android mobile development has been Kotlin-first since Google I/O in 2019. Over 50% of professional Android developers use Kotlin as their primary language, while only 30% use Java as their main language. 3 70% of developers whose primary language is Kotlin say that Kotlin makes them more productive. Using Kotlin for Android development, you can benefit from: Less code combined with greater readability. Fewer common errors. Kotlin support in Jetpack libraries. Support for multiplatform development. Mature language and environment. Easy learning. Big community. 4 Kotlin for Android | Kotlin Documentation Open Kotlin Playground Kotlin Playground | Android Developers 5 Kotlin Basics 01 The smallest Kotlin program. fun main() { println("Hello, world!") } 7 2022 Nora Alhelal Define versus call a function 8 use the special word fun for function Function names should follow the camel case convention, fun greeting(): String { val greet = "Hello" var name = "Nora" return "”$greet , weclome $name !" } fun sum(a: Int, b: Int): Int { return a + b } Fix errors in your code fun main() { 11 println("Today is sunny!) } fun birthdayGreeting(): String { A 'return' expression println("Happy Birthday, Rover!") required in a function println("You are now 5 years old!")with a block body ('{...}') } Variables Kotlin uses two different keywords to declare variables: val and var. Use val for a variable whose value never changes. Use var for a variable whose value can change. 12 Use a variable fun main() { val count: Int = 2 Run the program println("You have count unread messages.") } Fix it: 13 fun main() { val count: Int = 2 println("You have $count unread messages.") } Type inference val a: Int = 1 // immediate assignment val b = 2.30 // `Double` type is inferred val c: Int // Type required when no initializer is provided c=3 // deferred assignment Functions with multiple parameters Normal Function Call Creating classes and instances class Rectangle(var height: Double, var length: Double) { var perimeter = (height + length) * 2 } val rectangle = Rectangle(5.0, 2.0) println("The perimeter is ${rectangle.perimeter}") Kotlin Basics Control flow Loop example examples val guests = 30 if (guests == 0) { println("No guests") } else if (guests < 20) { println("Small group of people") } else { println("Large group of people!") } Nora Alhelal Range example for (i in 1..5) print(i) ⇒ 12345 for (i in 5 downTo 1) print(i) ⇒ 54321 for (i in 3..6 step 2) print(i) ⇒ 35 19 for (i in 'd'..'g') print (i) ⇒ defg Nora Alhelal Kotlin Basics listOf() mutableListOf() arrayOf() val instruments = val myList = val pets = arrayOf("dog", "cat", "canary") listOf("trumpet", "piano", mutableListOf("trumpet", "violin") "piano", "violin") println(java.util.Arrays.toString( println(instruments) myList.remove("violin") pets)) ⇒ [trumpet, piano, violin] ⇒ [dog, cat, canary] Lists created with listOf() use mutableListOf() to Once you create an array, cannot be changed create a list that can be the 20 size is fixed (immutable). changed An array can contain different An array can also contain just one type (integers in types. this case). val mix = arrayOf("hats", 2023 2) Nora Alhelal val numbers = intArrayOf(1, 2, 3) Null safety: Nullable values and null checks In Kotlin, variables cannot be null by default var numberOfBooks: Int = null ⇒ error: null can not be a value of a non-null type Int You can explicitly assign a variable to null using the safe call operator The safe ? call operator (?), after the type indicates that a variable can be null. Declare an Int? as nullable var numberOfBooks: Int? = null If you’re certain a variable won’t be null, use !! to force the variable into a non-null type. tests with the ?: operator. numberOfBooks = numberOfBooks?.dec() ?: 0 Nora Alhelal Type checks and automatic casts The is operator checks if an expression is an instance of a type. If an immutable local variable or property is checked for a specific type, there's no need to cast it explicitly: 22 Android Studio 02 2022 Nora Alhelal 23 Download, Open, and Download Android Studio Explore https://developer.android.com/studio Android Studio How to: Download/install Android Studio Nora Alhelal 24 Learn the Kotlin programming language | An droid Developers Basic syntax | Kotlin Documentation

Use Quizgecko on...
Browser
Browser