Lesson 1 Kotlin Basics
Lesson 1 Kotlin Basics
Kotlin basics
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 1: Kotlin basics
○ Get started
○ Operators
○ Data types
○ Variables
○ Conditionals
○ Lists and arrays
○ Null safety
○ Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Get started
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Open IntelliJ IDEA
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Create a new project
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Name the project
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Open REPL (Read-Eval-Print-Loop)
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Create a printHello() function
Press Control+Enter
(Command+Enter on
a Mac) to execute.
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Operators
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Operators
● Mathematical operators + - * / %
● Assignment operator =
● Equality operators == !=
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Math operators with integers
1 + 1 => 2
53 - 3 => 50
50 / 10 => 5
9 % 3 => 0
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Math operators with doubles
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Math operators
1+1 1.0/2.0
⇒ kotlin.Int = 2 ⇒ kotlin.Double = 0.5
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Numeric operator methods
Kotlin keeps numbers as primitives, but lets you call methods on numbers
as if they were objects.
2.times(3)
⇒ kotlin.Int = 6
3.5.plus(4)
⇒ kotlin.Double = 7.5
2.4.div(2)
⇒ kotlin.Double =
1.2
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Data types
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Integer types
Type Bits Notes
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Floating-point and other numeric types
Type Bits Notes
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Operand types
Results of operations keep the types of the operands
6*50 1/2
⇒ kotlin.Int = 300 ⇒ kotlin.Int = 0
6.0*50.0 1.0*2.0
⇒ kotlin.Double = 300.0 ⇒ kotlin.Double = 0.5
6.0*50
⇒ kotlin.Double = 300.0
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Type casting
Assign an Int to a Byte
val i: Int = 6
val b: Byte = i
println(b)
⇒ error: type mismatch: inferred type is Int but Byte was expected
Convert Int to Byte with casting
val i: Int = 6
println(i.toByte())
⇒ 6
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Underscores for long numbers
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Strings
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
String concatenation
val numberOfDogs = 3
val numberOfCats = 2
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
String templates
A template expression starts with a dollar sign ($) and can be a simple value:
val i = 10
println("i = $i")
=> i = 10
val s = "abc"
println("$s.length is ${s.length}")
=> abc.length is 3
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
String template expressions
val numberOfShirts = 10
val numberOfPants = 5
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Variables
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Variables
● Powerful type inference
● Let the compiler infer the type
● You can explicitly declare the type if needed
● Mutable and immutable variables
● Immutability not enforced, but recommended
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Specifying the variable type
Colon Notation
var width: Int = 12
var length: Double = 2.5
Important: Once a type has been assigned by you or the compiler, you can't
change the type or you get an error.
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Mutable and immutable variables
● Mutable (Changeable)
var score = 10
● Immutable (Unchangeable)
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
var and val
var count = 1
count = 2
val size = 1
size = 2
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Conditionals
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Control flow
● If/Else statements
● When statements
● For loops
● While loops
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
if/else statements
val numberOfCups = 30
val numberOfPlates = 50
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
if statement with multiple cases
val guests = 30
if (guests == 0) {
println("No guests")
} else if (guests < 20) {
println("Small group of people")
} else {
println("Large group of people!")
}
⇒ Large group of people!
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Ranges
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Ranges in if/else statements
val numberOfStudents = 50
if (numberOfStudents in 1..100) {
println(numberOfStudents)
}
=> 50
Note: There are no spaces around the "range to" operator (1..100)
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
when statement
when (results) {
0 -> println("No results")
in 1..39 -> println("Got results!")
else -> println("That's a lot of results!")
}
⇒ That's a lot of results!
As well as a when statement, you can also define a when expression that
provides a return value.
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
for loops
You don’t need to define an iterator variable and increment it for each pass.
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
for loops: elements and indexes
}
⇒ Item at 0 is dog
Item at 1 is cat
Item at 2 is canary
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
for loops: step sizes and ranges
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
while loops
var bicycles = 0
while (bicycles < 50) {
bicycles++
}
println("$bicycles bicycles in the bicycle rack\n")
⇒ 50 bicycles in the bicycle rack
do {
bicycles--
} while (bicycles > 50)
println("$bicycles bicycles in the bicycle rack\n")
⇒ 49 bicycles in the bicycle rack
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
repeat loops
repeat(2) {
print("Hello!")
}
⇒ Hello!Hello!
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Lists and arrays
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Lists
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Immutable list using listOf()
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Mutable list using mutableListOf()
⇒ kotlin.Boolean = true
With a list defined with val, you can't change which list the variable refers to, but
you can still change the contents of the list.
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Arrays
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Array using arrayOf()
With an array defined with val, you can't change which array the variable
refers to, but you can still change the contents of the array.
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Arrays with mixed or single types
An array can also contain just one type (integers in this case).
val numbers = intArrayOf(1, 2, 3)
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Combining arrays
=> [4, 5, 6, 1, 2, 3]
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Null safety
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Null safety
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Variables cannot be null
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Safe call operator
The safe call operator (?), after the type indicates that a variable can be null.
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Testing for null
Check whether the numberOfBooks variable is not null. Then decrement that
variable.
var numberOfBooks = 6
if (numberOfBooks != null) {
numberOfBooks = numberOfBooks.dec()
}
Now look at the Kotlin way of writing it, using the safe call operator.
var numberOfBooks = 6
numberOfBooks = numberOfBooks?.dec()
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
The !! operator
If you’re certain a variable won’t be null, use !! to force the variable into a non-
null type. Then you can call methods/properties on it.
Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Elvis operator
The ?: operator is sometimes called the "Elvis operator," because it's like a smiley
on its side with a pompadour hairstyle, like Elvis Presley styled his hair.
Android Development with Kotlin This work is licensed under the Apache 2 license. 56
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 57
Summary
In Lesson 1, you learned how to:
● Create an IntelliJ IDEA project, opening REPL, and execute a function
● Use operators and numeric operator methods
● Use data types, type casting, strings, and string templates
● Use variables and type inference, and mutable and immutable
variables
● Use conditionals, control flow, and looping structures
● Use lists and arrays
● Use Kotlin's null safety features
Android Development with Kotlin This work is licensed under the Apache 2 license. 58
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 59