0% found this document useful (0 votes)
64 views

Learn Kotlin - Data Types & Variables Cheatsheet - Codecademy

The document summarizes key data types and operations in Kotlin including mutable and immutable variables, type inference, string concatenation and templates, arithmetic operators, and functions from the math library. Mutable variables declared with 'var' can change, immutable with 'val' cannot. When declaring a variable without a type, Kotlin uses type inference. Strings can be concatenated with '+' and contain variables with '$'. Operators include addition, subtraction, multiplication, division, and modulus. Evaluation follows order of operations rules.

Uploaded by

Diprot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Learn Kotlin - Data Types & Variables Cheatsheet - Codecademy

The document summarizes key data types and operations in Kotlin including mutable and immutable variables, type inference, string concatenation and templates, arithmetic operators, and functions from the math library. Mutable variables declared with 'var' can change, immutable with 'val' cannot. When declaring a variable without a type, Kotlin uses type inference. Strings can be concatenated with '+' and contain variables with '$'. Operators include addition, subtraction, multiplication, division, and modulus. Evaluation follows order of operations rules.

Uploaded by

Diprot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Learn Kotlin

Data Types & Variables


Mutable Variables
A mutable variable is declared with the var keyword
and represents a value that is expected to change var age = 25
throughout a program. age = 26  

Immutable Variables
An immutable variable is declared with the val
keyword and represents a value that must remain val goldenRatio = 1.618
constant throughout a program.

Type Inference
When a data type is not speci ed 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

var color = "Purple"

String Concatenation
String concatenation is the process of combining
Strings using the + operator. var streetAddress = "123 Main St."
var cityState = "Brooklyn, NY"

println(streetAddress + " "


+ cityState)
// Prints: 123 Main St. 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 var monument = "the Statue of
length property returns the number of characters Liberty"
in a String, and the capitalize() function
capitalizes the rst letter of a String. println(monument.capitalize()) //
Prints: The Statue of Liberty
println(monument.length) // Prints:
21

Character Escape Sequences


Character escape sequences consist of a backslash and
character and are used to format text. print("\"Excellent!\" I cried.
\"Elementary,\" said he.")
● \n Inserts a new line
● // Prints: "Excellent!" I cried.
\t Inserts a tab
"Elementary," said he.  
● \r Inserts a carriage return
● \' Inserts a single quote
● \" Inserts a double quote
● \\ Inserts a backslash
● \$ Inserts the dollar symbol

Arithmetic Operators
The arithmetic operators supported in Kotlin include
+ addition, - subtraction, * multiplication, / 5 + 7  // 12
division, 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

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
arithmetic and assignment operator used to calculate var batteryPercentage = 80
and reassign a value in one step.
// Long Syntax
batteryPercentage = batteryPercantage
+ 10

// Short Syntax with an Augmented


Assignment Operator
batteryPercentage += 10

Increment and Decrement Operators


Increment and decrement operators provide a
shorthand syntax for adding or subtracting 1 from a var year = 2019
value. An increment operator consists of two year++ // 2020
consecutive plus symbols, ++ , meanwhile a year-- // 2019
decrement operator consists of two consecutive minus
symbols, -- .

The Math Library


The Math library, inherited from Java, contains
various mathematical functions that can be used within Math.pow(2.0, 3.0)  // 8.0
a Kotlin program. Math.min(6, 9)      // 6 
Math.max(10, 12)    // 12
Math.round(13.7)    // 14

You might also like