1. This work is licensed under the Apache 2.0 License
Kotlin 101
2. Write better Android apps faster with Kotlin. Kotlin is a
modern statically typed programming language used by over
60% of professional Android developers that helps boost
productivity, developer satisfaction, and code safety.
This work is licensed under the Apache 2.0 License
3. This work is licensed under the Apache 2.0 License
Kotlin has been designed in a way that it eliminates the need for
boilerplate code. Kotlin requires fewer lines of code as compared to
java.Google got Kotlin for free because it is released under Apache
2.0 open-source license
Why the new language?
4. This work is licensed under the Apache 2.0 License
Differences
In Java, extension functions are not available.
Thus, java supports
implicit conversion.
Developers need to define the
constructor, getter, and setter
methods in Java to create data classes.
Java does not allow
language scripting.
Kotlin helps developers easily
add extension functions in existing classes..
Kotlin does not support implicit
conversion of data types.
Kotlin compiler creates constructor,
getter, and setter methods automatically
Kotlin enables language scripting
integration in existing scripts.
5. This work is licensed under the Apache 2.0 License
Let’s start learning ….
7. This work is licensed under the Apache 2.0 License
Types
Kotlin uses two different keywords to declare variables: val and var.
● Use val for a variable whose value never changes. You can't assign a
value to a variable that was declared using val.
● Use var for a variable whose value can change.
8. This work is licensed under the Apache 2.0 License
Use of var
9. This work is licensed under the Apache 2.0 License
Use of val
10. This work is licensed under the Apache 2.0 License
Use of null
11. This work is licensed under the Apache 2.0 License
Conditional Statements
12. This work is licensed under the Apache 2.0 License
Lets try an example
In life, it's common to do things differently based on the situation that you face. For
example, if the weather is cold, you wear a jacket, whereas if the weather is warm, you
don't wear a jacket.
13. This work is licensed under the Apache 2.0 License
Decision-making is also a fundamental concept in programming. You
write instructions about how a program should behave in a given
situation so that it can act or react accordingly when the situation occurs.
In Kotlin, when you want your program to perform different actions
based on a condition, you can use an if/else statement
14. This work is licensed under the Apache 2.0 License
Here the function main has println function which tests
for the condition that if 1 is equal to 1 , which is indeed
true. Hence results true.
15. This work is licensed under the Apache 2.0 License
Some basic relational operators
● Less than: <
● Greater than: >
● Less than or equal to:
<=
● Greater than or equal
to: >=
● Not equal to: !=
16. This work is licensed under the Apache 2.0 License
Some more code snippets
17. This work is licensed under the Apache 2.0 License
After the closing curly brace of the if statement, you add the else keyword followed by a pair of curly braces. Inside
the curly braces of the else statement, you can add a second body that only executes when the condition in the if
branch is false
18. This work is licensed under the Apache 2.0 License
Code flowchart
19. This work is licensed under the Apache 2.0 License
20. This work is licensed under the Apache 2.0 License
Different structuring of if-else
block
21. This work is licensed under the Apache 2.0 License
22. This work is licensed under the Apache 2.0 License
Example snippet
23. This work is licensed under the Apache 2.0 License
In Kotlin, when you deal with multiple branches, you can use the when statement
instead of the if/else statement because it improves readability, which refers to how
easy it is for human readers, typically developers, to read the code. It's very
important to consider readability when you write your code because it's likely that
other developers need to review and modify your code throughout its lifetime
When keyword
24. This work is licensed under the Apache 2.0 License
Flowchart
when statements are preferred
when there are more than two
branches to consider.
25. This work is licensed under the Apache 2.0 License
26. This work is licensed under the Apache 2.0 License
27. This work is licensed under the Apache 2.0 License
Optimised code
The prime-number program contains a lot of repetition of println()
statements. When you write a when statement, you can use a comma (,) to
denote multiple conditions that correspond to the same body.
28. This work is licensed under the Apache 2.0 License
29. This work is licensed under the Apache 2.0 License
Besides the comma (,) symbol to denote multiple conditions, you can also use the in
keyword and a range of values in when branches.To use a range of values, add a number
that denotes the start of the range followed by two periods without any spaces and then
close it with another number that denotes the end of the range.
In keyword
30. This work is licensed under the Apache 2.0 License
31. This work is licensed under the Apache 2.0 License
Converting if-else to expression
33. This work is licensed under the Apache 2.0 License
Example scenario
● You learned that when you declare a variable, you need to assign it a
value immediately. For example, when you declare a favoriteActor
variable, you may assign it a "Sandra Oh" string value immediately.
● However, what if we don't
have a favorite actor? You
might want to assign the
variable a "Nobody" or
"None" value. This isn't a good approach because your program
interprets the favoriteActor variable to have a "Nobody" or "None"
value rather than no value at all. In Kotlin, you can use null to
indicate that there's no value associated with the variable.
34. Null keyword
This work is licensed under the Apache 2.0 License
Now favouriteactor has been
assigned null
35. This work is licensed under the Apache 2.0 License
36. This work is licensed under the Apache 2.0 License
So why did it happen?
A type is only nullable if you explicitly let it hold null. As the error message says, the String data type is a
non-nullable type, so you can't reassign the variable to null.To declare nullable variables in Kotlin, you
need to add a ? operator to the end of the type. For example, a String? type can hold either a string or
null, whereas a String type can only hold a string. To declare a nullable variable, you need to explicitly
add the nullable type. Without the nullable type, the Kotlin compiler infers that it's a non-nullable type.
37. This work is licensed under the Apache 2.0 License
38. This work is licensed under the Apache 2.0 License
Functions
39. This work is licensed under the Apache 2.0 License
Store function in a variable
40. This work is licensed under the Apache 2.0 License
Error is thrown
Store function in a variable
It produces an error because the
Kotlin compiler recognizes trick as the
name of the trick() function, but
expects to call the function, rather
than assign it to a variable.
41. This work is licensed under the Apache 2.0 License
Store function in a variable
Reference operator (: :)
42. This work is licensed under the Apache 2.0 License
Practice:
Kotlin
Fundamentals
43. This work is licensed under the Apache 2.0 License
Write a program that prints the summary message based on the number
of notifications that you received. The message should include:
● The exact number of notifications when there are less than 100 notifications.
● [99+] as the number of notifications when there are 100 notifications or more.
You have 51 notifications.
Your phone is blowing up! You have 99+ notifications.
44. This work is licensed under the Apache 2.0 License
45. This work is licensed under the Apache 2.0 License
Movie tickets are typically priced differently based on the age of moviegoers.
In the initial code provided in the following code snippet, write a program that
calculates these age-based ticket prices:
● A children's ticket price of $15 for people 12 years old or younger.
● A standard ticket price of $30 for people between 13 and 60 years old. On Mondays,
discount the standard ticket price to $25 for this same age group.
● A senior ticket price of $20 for people 61 years old and older. Assume that the
maximum age of a moviegoer is 100 years old.
● A -1 value to indicate that the price is invalid when a user inputs an age outside of the
age specifications.
The movie ticket price for a person aged 5 is $15.
The movie ticket price for a person aged 28 is $25.
The movie ticket price for a person aged 87 is $20.
46. This work is licensed under the Apache 2.0 License
47. This work is licensed under the Apache 2.0 License
There are three main temperature scales used in the world: Celsius,
Fahrenheit, and Kelvin.
In the initial code provided in the following code snippet, write a
program that converts a temperature from one scale to another with
these formulas:
● Celsius to Fahrenheit: ° F = 9/5 (° C) + 32
● Kelvin to Celsius: ° C = K - 273.15
● Fahrenheit to Kelvin: K = 5/9 (° F - 32) + 273.15
27.0 degrees Celsius is 80.60 degrees Fahrenheit.
350.0 degrees Kelvin is 76.85 degrees Celsius.
10.0 degrees Fahrenheit is 260.93 degrees Kelvin.
48. This work is licensed under the Apache 2.0 License
49. This work is licensed under the Apache 2.0 License
Classes
50. This work is licensed under the Apache 2.0 License
Structure
● A class definition starts with the
class keyword, followed by a name
and a set of curly braces
51. This work is licensed under the Apache 2.0 License
Structure
● A class definition starts with the
class keyword, followed by a name
and a set of curly braces
● Class an an object``
52. This work is licensed under the Apache 2.0 License
Class Properties
Device name is: Android TV
Smart device is turned on.
Smart device is turned off.
53. This work is licensed under the Apache 2.0 License
Constructors
54. This work is licensed under the Apache 2.0 License
THANK YOU
Have fun encouraging your
community in becoming
Android Developers!