Kotlin
Introduction
Kotlin is a modern statically typed programming language
Expressive and concise
Safer code (provides nullability check)
Supports asynchronous programming using coroutines
Jetpack Compose (Android's modern UI toolkit) uses Kotlin
https://kotlinlang.org/docs/home.html
Apps built with Kotlin
Online Editor
https://play.kotlinlang.org/
Command Line Compiler
https://kotlinlang.org/docs/command-line.html
Variable declaration
Kotlin uses two different keywords to declare variables: val and var
Use val for a variable whose value never changes
You can't re-assign a value to a variable that was declared using val
Use var for a variable whose value can change
Variable declaration
In the example below, count is a variable of type Int that is assigned an
initial value of 10:
var count: Int = 10
Int is a type that represents an integer, one of the many numerical types that
can be represented in Kotlin
Similar to other languages, you can also use Byte, Short, Long, Float, and
Double depending on your numerical data
Variable declaration
The var keyword means that you can reassign values to count as needed
For example, you can change the value of count from 10 to 15:
var count: Int = 10
count = 15
Variable declaration
If you do not want to change the value you can use val
val languageName: String = "Kotlin"
Type inference
The Kotlin compiler can infer the type based off of the type of the assigned
value
val languageName = "Kotlin"
val upperCaseName = languageName.toUpperCase()
// Fails to compile
languageName.inc()
Null safety
Kotlin variables can't hold null values by default
// Fails to compile
val languageName: String = null
For a variable to hold a null value, it must be of a nullable type
You can specify a variable as being nullable by suffixing its type with ?
val languageName: String? = null
Null safety
With a String? type, you can assign a nullable variable either a String value
or null
You must handle nullable variables carefully or a NullPointerException
might happen
Conditionals
If Expression
When Expression
Smart Casting
Loops
Loops
Ranges
Collections
Type checks and automatic casts
Type checks and automatic casts
Type checks and automatic casts
Functions
Functions
Functions
Functions
fun generateAnswerString(countThreshold: Int): String = if
(count > countThreshold) {
"I have the answer"
} else {
"The answer eludes me"
}
Anonymous functions
Classes
class Car
Properties
Class functions and encapsulation
Class functions and encapsulation
References
https://kotlinlang.org/docs/basic-syntax.html#collections
https://kotlinlang.org/docs/kotlin-tour-welcome.html
https://kotlinlang.org/
Exercise: Variable
Write a Kotlin program to declare a variable currentTime of type String and
assign it a value representing the current time
Declare a variable temperature and assign a value of type Double
representing the current temperature
Declare a variable grade and assign it a Char representing a student's grade
(e.g., 'A', 'B', 'C', etc.)
Exercise: Conditionals
Write a program that determines if a triangle is equilateral, isosceles, or
scalene based on the lengths of its sides
Implement a program that calculates the total salary of an employee based on
the number of hours worked and the hourly rate, considering overtime
Exercise: Conditionals
Implement a program that determines the season based on the month and
day entered
Write a program that finds the largest among three different numbers using
nested conditionals
Exercise: Functions
Implement a function that sorts an array of integers in ascending order
Implement a function that calculates the factorial of a given number using
recursion
Develop a function that checks if a given string contains only unique
characters
Exercise: Loops
Implement a program to find the sum of all even numbers from 1 to 50 using
a for loop
Develop a program to find the prime numbers within a given range using a
while loop
Implement a program to check if a given number is a palindrome using a while
loop
Exercise: Class
Develop a class representing a Rectangle with properties for length and width
and methods to calculate its perimeter and area
Create a class representing a Triangle with properties for three sides and
methods to determine if it's equilateral, isosceles, or scalene
Develop a class to model a ShoppingCart with properties for items and
methods for adding, removing, and calculating total price