12/12/23, 12:35 PM Learn Kotlin: Data Types & Variables Cheatsheet | Codecademy
Cheatsheets / Learn Kotlin
Data Types & Variables
Arithmetic Operators
The arithmetic operators supported in Kotlin include + 5 + 7 // 12
addition, - subtraction, * multiplication, / division,
9 - 2 // 7
and % modulus.
8 * 4 // 32
25 / 5 // 5
31 % 2 // 1
Order of Operations
The order of operations for compound arithmetic 5 + 8 * 2 / 4 - 3 // 6
expressions is as follows:
3 + (4 + 4) / 2 // 7
1. Parentheses
2. Multiplication 4 * 2 + 1 * 7 // 15
3. Division 3 + 18 / 2 * 1 // 12
4. Modulus
6 - 3 % 2 + 2 // 7
5. Addition
6. Subtraction
When an expression contains operations such as
multiplication and division or addition and subtraction
side by side, the compiler will evaluate the expression in a
left to right order.
Augmented Assignment Operators
An augmented assignment operator includes a single var batteryPercentage = 80
arithmetic and assignment operator used to calculate and
reassign a value in one step.
// Long Syntax
batteryPercentage = batteryPercantage + 10
// Short Syntax with an Augmented
Assignment Operator
batteryPercentage += 10
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-data-types-variables/cheatsheet 1/4
12/12/23, 12:35 PM Learn Kotlin: Data Types & Variables Cheatsheet | Codecademy
Increment and Decrement Operators
Increment and decrement operators provide a shorthand var year = 2019
syntax for adding or subtracting 1 from a value. An
year++ // 2020
increment operator consists of two consecutive plus
symbols, ++ , meanwhile a decrement operator consists year-- // 2019
of two consecutive minus symbols, -- .
The Math Library
The Math library, inherited from Java, contains various Math.pow(2.0, 3.0) // 8.0
mathematical functions that can be used within a Kotlin
Math.min(6, 9) // 6
program.
Math.max(10, 12) // 12
Math.round(13.7) // 14
Mutable Variables
A mutable variable is declared with the var keyword and var age = 25
represents a value that is expected to change throughout
age = 26
a program.
Immutable Variables
An immutable variable is declared with the val keyword val goldenRatio = 1.618
and represents a value that must remain constant
throughout a program.
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-data-types-variables/cheatsheet 2/4
12/12/23, 12:35 PM Learn Kotlin: Data Types & Variables Cheatsheet | Codecademy
Type Inference
When a data type is not specified in a variable // The following variable is assigned a
declaration, the variable’s data type can be inferred
text value within double quotes, thus the
through type inference.
inferred type is String
var color = "Purple"
String Concatenation
String concatenation is the process of combining Strings var streetAddress = "123 Main St."
using the + operator.
var cityState = "Brooklyn, NY"
println(streetAddress + " " + cityState)
// Prints: 123 Main St. Brooklyn, NY
String Templates
String templates contain String values along with variables var address = "123 Main St. Brooklyn, NY"
or expressions preceded by a $ symbol.
println("The address is $address")
// Prints: The address is 123 Main St.
Brooklyn, NY
Built-in Properties and Functions
The Kotlin String and Character data types contain var monument = "the Statue of Liberty"
various built-in properties and functions. The length
property returns the number of characters in a String,
and the capitalize() function capitalizes the first letter println(monument.capitalize()) // Prints:
of a String. The Statue of Liberty
println(monument.length) // Prints: 21
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-data-types-variables/cheatsheet 3/4
12/12/23, 12:35 PM Learn Kotlin: Data Types & Variables Cheatsheet | Codecademy
Character Escape Sequences
Character escape sequences consist of a backslash and print("\"Excellent!\" I cried.
character and are used to format text.
\"Elementary,\" said he.")
\n Inserts a new line
\t Inserts a tab
\r Inserts a carriage return // Prints: "Excellent!" I cried.
\' Inserts a single quote "Elementary," said he.
\" Inserts a double quote
\\ Inserts a backslash
\$ Inserts the dollar symbol
Print Share
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-data-types-variables/cheatsheet 4/4