Learn Kotlin - Data Types & Variables Cheatsheet - Codecademy
Learn Kotlin - Data Types & Variables Cheatsheet - Codecademy
Immutable Variables
An immutable variable is declared with the val keyword
and represents a value that must remain constant val goldenRatio = 1.618
throughout a program.
Type Inference
When a data type is not specified in a variable
declaration, the variable’s data type can be inferred // The following variable is assigned
through type inference. a text value within double quotes, thus
the inferred type is String
String Concatenation
String concatenation is the process of combining Strings
using the + operator. var streetAddress = "123 Main St."
var cityState = "Brooklyn, NY"
String Templates
String templates contain String values along with variables
or expressions preceded by a $ symbol. var address = "123 Main St. Brooklyn, NY"
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
various built-in properties and functions. The length var monument = "the Statue of Liberty"
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
● \\ Inserts a backslash
Arithmetic Operators
The arithmetic operators supported in Kotlin include +
addition, - subtraction, * multiplication, / division, 5 + 7 // 12
and % modulus. 9 - 2 // 7
8 * 4 // 32
25 / 5 // 5
31 % 2 // 1
Order of Operations
The order of operations for compound arithmetic
expressions is as follows: 5 + 8 * 2 / 4 - 3 // 6
3 + (4 + 4) / 2 // 7
1. Parentheses
4 * 2 + 1 * 7 // 15
2. Multiplication 3 + 18 / 2 * 1 // 12
3. Division 6 - 3 % 2 + 2 // 7
4. Modulus
5. Addition
6. Subtraction