Kotlin Exercise Sec 1
Kotlin Exercise Sec 1
Introduction
Kotlin is a modern statically typed programming language
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
You can't re-assign a value to a variable that was declared using val
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:
count = 15
Variable declaration
If you do not want to change the value you can use val
// Fails to compile
languageName.inc()
Null safety
Kotlin variables can't hold null values by default
// Fails to compile
You can specify a variable as being nullable by suffixing its type with ?
} else {
}
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
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
Develop a program to find the prime numbers within a given range using a
while loop
Create a class representing a Triangle with properties for three sides and
methods to determine if it's equilateral, isosceles, or scalene