Kotlin Notes (1)
Kotlin Notes (1)
er
correct and adheres to the rules of the
programming language. Once the translation
process is complete, the resulting machine code
can be executed by the computer's processor.
Compilers are essential tools in software
development, enabling programmers to write code
in familiar languages while allowing computers to
understand and execute it efficiently.
▪ An interpreter is a program that directly executes
source code written in a high-level programming
language without the need for prior translation into
machine code. It reads the source code line by line
and translates each line into machine code
instructions or intermediate code on-the-fly. The
interpreter then executes these instructions
immediately, allowing the program to run without
1. Execution:
1. Compiler: Produces a standalone executable file that can be executed independently of the
original source code.
2. Interpreter: Executes the source code directly without generating an independent executable
file.
COMPLIER 2. Performance:
• Compiler: Typically produces faster-executing code since it optimizes the entire program
VS •
before execution.
Interpreter: Generally slower as it translates and executes code line-by-line or statement-by-
INTERPRET
statement.
3. Debugging:
ER
1. Compiler: Debugging can be more challenging as errors may only be detected during
compilation or runtime.
2. Interpreter: Easier to debug as errors are reported immediately at the line causing the issue.
4. Portability:
1. Compiler: Generates machine-specific code, so the compiled executable may not run on
different architectures without recompilation.
2. Interpreter: Source code is generally portable, as long as the interpreter is available for the
target platform.
• Examples:
• Compiler: Examples include GCC (GNU Compiler Collection), Microsoft Visual C++, and LLVM.
1. Interpreter: Examples include Python, JavaScript (in web browsers), and Ruby.
▪ Kotlin is a modern, trending programming language
that was released in 2016 by JetBrains.
▪ It has become very popular since it is compatible with
Java (one of the most popular programming languages
out there), which means that Java code (and libraries)
can be used in Kotlin programs.
What is ▪ Kotlin is used for:
Kotlin • Mobile applications (specially Android apps)
• Web development
• Server side applications
• Data science
• And much, much more!
• Kotlin is fully compatible with Java
• Kotlin works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc.)
• Kotlin is concise and safe
Use of • Kotlin is easy to learn, especially if you already
Kotlin know Java
• Kotlin is free to use
• Big community/support
▪ fun main()
▪ {
▪ println("Hello World")
▪ }
▪ The fun keyword is used to declare a function. A function is a
block of code designed to perform a particular task. In the
Kotlin example above, it declares the main() function.
Syntax ▪ The main() function is something you will see in every Kotlin
program. This function is used to execute code. Any code
inside the main() function's curly brackets {} will
be executed.
▪ For example, the println() function is inside
the main() function, meaning that this will be executed.
The println() function is used to output/print text, and in our
example it will output "Hello World".
▪ fun main()
▪{
▪ println("Hello World!")
Some ▪ println("I am learning Kotlin.")
Examples ▪ println("It is awesome!")
▪ println(3 + 3)
▪}
▪ Single-line comments starts with two forward
slashes (//).
▪ Any text between // and the end of the line is
Variables ▪Syntax
▪ var variableName = value
▪ val variableName = value
▪ var name = "John"
▪ val birthyear = 1975
▪ println(name) // Print the value of name
Example println(birthyear) // Print the value of birthyear
▪ The difference between var and val is that
variables declared with the var keyword can be
changed/modified, while val variables cannot.
▪ val name = "John"
▪ println("Hello " + name)
▪ val firstName = "John ”
▪ val lastName = "Doe"
Variables ▪ println(fullName)
▪ val x = 5 val
▪y=6
▪ println(x + y)
▪ // Print the value of x + y
▪ val myNum = 5 // Int
▪ val myDoubleNum = 5.99 // Double
100 + 50
Table 1
OpeNam Description Exa
ratoe mpl
Arithmetic r
+ Additi Adds
e
x+
Operators on together two y
values
- Subtr Subtracts x-
actio one value y
n from another
* Multi Multiplies x*
plicat two values y
ion
/ Divisi Divides one x /
on value from y
another
% Modu Returns the x %
lus division y
Operator Example Same As
Operators += x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
Operator Name Example
Equal to x == y
!= Not equal x != y
Syntax •Example
if (20 > 18) {
println("20 is greater than 18")
}
val x = 20
val y = 18
Example if (x > y) {
println("x is greater than y")
}
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
While Loop Loops are handy because they save time, reduce errors, and
they make code more readable.
while (condition) {
// code block to be executed
}
var i = 0
while (i < 5) {
Example println(i)
i++
}
The do..while loop is a variant of the while loop. This loop will
execute the code block once, before checking if the condition is
true, then it will repeat the loop as long as the condition is true.
Do-while do {
// code block to be executed
}
while (condition);
var i = 0
do {
Example println(i)
i++
}
while (i < 5)
Arrays are used to store multiple values in a single variable,
instead of creating separate variables for each value.
Arrays To create an array, use the arrayOf() function, and place the
values in a comma-separated list inside it:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
Accessing You can access an array element by referring to the index
number, inside square brackets.
Elements In this example, we access the value of the first element in cars:
In an Array val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
println(cars[0])
Changing ▪To change the value of a specific element, refer to
Elements the index number:
In an array cars[0] = "Opel"
Array Length or
Size val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
println(cars.size)
Checking val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
if ("Volvo" in cars) {
Elements println("It exists!")
} else {
In Array println("It does not exist.")
}
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
Loop Through for (x in cars) {
println(x)
Array }
A function is a block of code which only runs when it is
called.
println(c1.brand) // Ford
println(c2.brand)
Kotlin class Car(var brand: String, var model: String, var year: Int)
fun main() {
Example val c1 = Car("Ford", "Mustang", 1969)
val c2 = Car("BMW", "X5", 1999)
val c3 = Car("Tesla", "Model S", 2020)
}
class Car(var brand: String, var model: String, var year: Int) {
// Class function
fun drive() {
println("Wrooom!")
Class Functions }
}
or methods
fun main() {
val c1 = Car("Ford", "Mustang", 1969)
fun main() {
val c1 = Car("Ford", "Mustang", 1969)
// Subclass
class MyChildClass: MyParentClass() {
fun myFunction() {
Inheritance println(x) // x is now inherited from the superclass
}
}
class Transactions:Bank{
override var balance: Double = 1000.0