Learn the Basics of the Kotlin Language

May 22 2024 · Kotlin 1.9, Android 14, Kotlin Playground 1.9

Lesson 02: Create Variables

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In software programming, variables hold data and have a type. In Kotlin, variables can be read-only or mutable. In this demo, you’ll see how to initialize and work with variables in Kotlin. Later in this video, you’ll learn about code comments too. Start a new Kotlin Playground session or choose your preferred Kotlin environment to get started.

Using Variables

A variable holds data and has a name. In Kotlin, this is how variables are declared:

var day = "Monday"
val week = 2
lateinit var day : String

fun main(args: Array<String>) {
  day = "Monday"
}

Mutable and Read-only Variables

To update a variable, it needs to be mutable. The previous example is mutable since it’s a var. To update it, remove the keyword and assign a different value of the same type:

fun main(args: Array<String>) {
  day = "Monday"
  println(day)

  day = "Tuesday"
  println(day)
}
val day = "Monday"
val day = "Monday"
day = "Tuesday" // Error

Basic Data Types

Basic types in Kotlin include integers, floating-point numbers, booleans, characters and strings.

Int

Integers represent numbers in Kotlin between -2,147,483,648 (-2^31^) and 2,147,483,647 (-2^31^ - 1). Here’s an example of an integer data type:

val amount = 100
println(amount::class.java.simpleName)

Long

If a value exceeds the limit for an Integer, it becomes a Long. Longs are also numeric values but have a higher capacity than Ints. Here’s an example of a Long:

val amount = 100L
println(amount::class.java.simpleName)

Floating-point

Floating-point types are numbers with decimals or fractions. Single-precision or decimal numbers holding 32 bits of data, are assigned the Floattype when creating variables. To initialize a floating point number explicitly, append an f to the number:

val amount = 100f
println(amount::class.java.simpleName)

Double

For higher or double-precision numeric data, use the Double class type. Double stores 64 bits of numeric data. To initialize a Double, use precision or a decimal point.”

val amount = 100.0
println(amount::class.java.simpleName)
val amount = 12_000_000_000
println(amount)

Boolean

The Boolean data type can be only one of two types: true or false. This makes it suitable for representing data that can only be in two states. In the next example, the isWeekday variable tells whether it’s a weekday or not whiles isHoliday tells whether it’s a holiday or not:

val isWeekday = true
val isHoliday = false

Character

Characters represent single-character symbols and numbers. They’re instantiated using single quotes ' and must contain one character or two if it’s a special character:

val grade = 'A'
val newLine = '\n'
print("Monday")
print(newLine)
print("Tuesday")

String

Strings represent text. Strings are a sequence of characters put together and are initialized with double quotes ". They may or may not contain any characters.

val message = "End of the program!\nSee you next week."
println(message)
val message = """
End of the program!
See you next week.
"""
println(message)

Comments

Comments are a way to leave notes in your code. Comments are not interpreted as part of the code and can be short or long. For short comments, write your notes after // followed by a space. This type of comment is usually left as a note to self:

// This comment is on top of the variable being described.
const val WEEKLY_INTEREST = 100 // This comment is beside the variable being described.
/** This is known
as a block comment. */
const val WEEKLY_INTEREST = 100
/** This comment is a longer comment giving further information about the variable. */
const val WEEKLY_INTEREST = 100
/**
* This comment is longer.
* It has more information.
*/
const val WEEKLY_INTEREST = 100
/*
This comment is longer.
It has more information.
This is a valid comment block, but cannot be used with automatic document creation tools.
*/
const val WEEKLY_INTEREST = 100

Creating Code Comments

Comments in programming can also exclude a piece of code. Comments aren’t interpreted by the compiler, so they won’t have any effect on the output of the program. Consider the following example:

// const val WEEKLY_INTEREST = 100 /* This code is said to be commented out.*/
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 5 Next: Conclusion