1. This work is licensed under the Apache 2.0 License
Kotlin Fundamentals
2. This work is licensed under the Apache 2.0 License
Compose apps are written in
the Kotlin programming
language.
Kotlin is the language that the
majority of professional Android
developers use to build apps.
3. This work is licensed under the Apache 2.0 License
Kotlin Playground
Write and run Kotlin code in
the browser.
https://play.kotlinlang.org/
4. This work is licensed under the Apache 2.0 License
Program
A series of instructions for a
computer to perform some
action.
fun main() {
println("Hello, world!")
}
Output:
Hello, world!
5. Functions
A function is a segment of a program that
performs a specific task.
You can have many functions in your program
6. This work is licensed under the Apache 2.0 License
main Function
The main function is the entry
point, or starting point, of the
program.
Start here
fun main() {
println("Hello, world!")
}
Output:
Hello, world!
7. This work is licensed under the Apache 2.0 License
Defining a function
Functions begin with the fun
keyword.
fun displayIntroduction() {
}
8. This work is licensed under the Apache 2.0 License
Defining a function
Functions have a name so that
they can be called.
fun displayIntroduction() {
}
9. This work is licensed under the Apache 2.0 License
Defining a function
Functions need a set of parentheses
after the function name in order to
surround the function inputs or
parameters.
fun displayIntroduction() {
}
10. This work is licensed under the Apache 2.0 License
Defining a function
The curly braces make up the
function body and contain the
instructions needed to execute
a task.
fun displayIntroduction() {
// Body
}
11. This work is licensed under the Apache 2.0 License
Putting it together
fun main() {
firstFunction() // Call a function
}
// Define a function
fun firstFunction() {
// Function Body
}
12. This work is licensed under the Apache 2.0 License
Basic data types
Kotlin Data type What kind of data it can contain Example literal values
String Text
“Add contact”
“Search”
Int Whole integer number
32
-59873
Double Decimal number
2.0
-37123.9999
Float
Decimal number (less precise than a Double).
Has an f or F at the end of the number.
5.0f
-1630.209f
Boolean
true or false. Use this data type when there
are only two possible values.
true
false
13. This work is licensed under the Apache 2.0 License
A container for a single piece of
data.
Variables
14. This work is licensed under the Apache 2.0 License
val keyword
Use when you expect the variable value will
not change.
Example: name
var keyword
Use when you expect the variable value can
change.
Example: age
Defining a variable
15. This work is licensed under the Apache 2.0 License
Defining a variable
Variables start with a var or val
keyword.
fun main() {
val name: String = "Bhavye"
var age: Int = 20
}
16. This work is licensed under the Apache 2.0 License
Defining a variable
All variables must have a name.
fun main() {
val name: String = "Meghan"
var age: Int = 28
}
17. This work is licensed under the Apache 2.0 License
Defining a variable
Data type is the type of data
that the variable holds.
fun main() {
val name: String = "Meghan"
var age: Int = 28
}
18. This work is licensed under the Apache 2.0 License
Defining a variable
The initial value is the value that
is stored in the variable.
fun main() {
val name: String = "Meghan"
var age: Int = 28
// val name = “Meghan”
// var age = 28
}
19. This work is licensed under the Apache 2.0 License
Defining a variable
If we want to define a variable
with no initial value.
fun main() {
val name: String
var age: Int
// var age => Error
}
Editor's Notes
To make it easier for you to learn, you’ll be writing your code in the Kotlin Playground which you can access via the web browser. The site looks something like this. You can write your code in this window and run it by hitting the green Run button. The result of your code (known as the output) will show up at the bottom of the window (where it says “Hello, world!”).
To illustrate a few important concepts that you’ll learn in this pathway, we will go through a short code demo to create a program in Kotlin.
In Kotlin Playground, you will learn to explore and modify simple programs in Kotlin. You can think of a program as a series of instructions for a computer or mobile device to perform some action. In this program, the action is printing “Hello, world!”.
A function is a segment of a program that performs a specific task. You can have many functions in your program or only a single one.
Creating separate functions for specific tasks has a number of benefits.
Reusable code: Rather than copying and pasting code that you need to use more than once, you can simply call a function wherever needed.
Readability: Ensuring functions do one and only one specific task helps other developers and teammates, as well as your future self to know exactly what a piece of code does.
A Kotlin program is required to have a main function, which is the entry point, or starting point, of the program.
You may be asking what a function is…
We will demonstrate how to define a function with a function called displayIntroduction() that we will use to print our name and age.
A function definition starts with the fun keyword. A keyword is a reserved word that has a special meaning in Kotlin, in this case the fun keyword tells Kotlin that you are going to make a function.
Functions need to have a descriptive name so that they can be called from other parts of the program.
Functions need a set of parentheses which you can use to optionally pass information into the function. displayIntroduction() won’t need information passed in. You will learn more about passing in inputs into functions later in the course.
Functions need curly braces that contain the instructions needed to execute a task.
Finally, we will replace the contents of the main() function with a call to the displayIntroduction() function when we run it, “Hi I’m Meghan and I am 28 years old” will print to the output.
In this lecture we went over functions and variables and how to put them together to create a function that introduces you. Soon you will go deeper into these concepts and try them out for yourself in the codelabs.
When you decide what aspects of your app can be variables, it's important to specify what type of data can be stored in those variables. In Kotlin, there are some common basic data types. This table shows a different data type in each row. For each data type, there's a description of what kind of data it can hold and example values.
A String holds text so you will use it to store your name, and an Int holds an integer number so you will use it to store your age.
In computer programming, a variable is a container for a single piece of data. You can envision it as a box that contains a value. The box has a label, which is the name of the variable. By referring to the box by its name, you have access to the value it holds.
Now, let’s jump into how you define a variable.
You can declare a variable using either val or var.
With val, the variable is read-only, which means you can only read, or access, the value of the variable. Once the value is set, you cannot edit or modify its value.
With var, the variable is mutable, which means the value can be changed or modified. The value can be mutated.
In Kotlin, it's preferred to use val over var when possible.
We will store your name as a val because that will not change.
We will store your age as a var because it changes every year.
To demonstrate how to define a variable we will define both name and age variables.
Before you use a variable, you must declare it. To declare a variable, start with the val or var keyword.
All variables must have a name that they can be referenced by.
The data type specifies the type of data that the variable holds. Note that a colon separates the name and data type.
In the variable declaration, the equal sign symbol (=) follows the data type. The equal sign symbol is called the assignment operator. The assignment operator assigns a value to the variable. The variable’s initial value is the data stored in the variable.
In the variable declaration, the equal sign symbol (=) follows the data type. The equal sign symbol is called the assignment operator. The assignment operator assigns a value to the variable. The variable’s initial value is the data stored in the variable.