\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Bochrak} \pdfinfo{ /Title (kotlin-fundamentals-for-android-development.pdf) /Creator (Cheatography) /Author (Bochrak) /Subject (Kotlin Fundamentals for Android Development Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{09AEBA} \definecolor{LightBackground}{HTML}{EFF9FA} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Kotlin Fundamentals for Android Development Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Bochrak} via \textcolor{DarkBackground}{\uline{cheatography.com/200241/cs/46213/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Bochrak \\ \uline{cheatography.com/bochrak} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 27th April, 2025.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Kotlin}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Kotlin is a {\bf{modern}}, {\bf{statically-typed programming language}} that runs on the {\bf{Java Virtual Machine (JVM)}} and can also be compiled to {\bf{JavaScript}} or {\bf{native code}}.\{\{nl\}\} \{\{fa-info-circle\}\} Developed by {\bf{JetBrains}} (the makers of IntelliJ IDEA) \{\{nl\}\} \{\{fa-info-circle\}\} Officially supported for {\bf{Android app development}} by {\bf{Google}} 2017 \{\{nl\}\} \{\{fa-info-circle\}\} Designed to be {\bf{concise}}, {\bf{safe}} and {\bf{interoperable with Java}}% Row Count 10 (+ 10) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Key Features}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-check-circle\}\} Concise syntax \{\{nl\}\} \{\{fa-check-circle\}\} Null Safety built-in \{\{nl\}\} \{\{fa-check-circle\}\} Fully interoperable with Java \{\{nl\}\} \{\{fa-check-circle\}\} Coroutines for lightweight concurrency \{\{nl\}\} \{\{fa-check-circle\}\} Multi-platform support (JVM, Android, Web, Native)% Row Count 6 (+ 6) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Variables}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-circle-right\}\} {\bf{var :}} {\emph{Mutable}} reference. The value can change during runtime. Its value can be reassigned after declaration. \{\{nl\}\}\{\{fa-chevron-circle-right\}\} {\bf{val :}} {\emph{Immutable}} reference. You cannot reassign a value once assigned. Read-only variables. \{\{nl\}\}\{\{nl\}\} \{\{fa-code\}\} {\bf{Immutable variables (val) - values cannot change}} \{\{nl\}\}`val drinkCount: Int` // ✅ Must specify Type since no initializer \{\{nl\}\} `drinkCount = 12 ` // ✅ Assigned later before usage \{\{nl\}\} `println(drinkCount)` // Output: 12 \{\{nl\}\} `drinkCount = 15` // ❌ Error: Val cannot be reassigned \{\{nl\}\}`val popcornBoxes = 5` // ✅ Kotlin infers Int type \{\{nl\}\}`val hotdogCount: Int = 7` // ✅ Explicitly declared as Int \{\{nl\}\} \{\{fa-code\}\} {\bf{Mutable variable (var) - value can change}} \{\{nl\}\}`var x = 5` // Mutable variable \{\{nl\}\} `x += 1` // ✅ Increments x by 1\{\{nl\}\} `println(x)` // Output: 6% Row Count 19 (+ 19) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{lateinit}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} Allows initializing a non-null variable later\{\{nl\}\} \{\{fa-chevron-up\}\} Used when you cannot initialize a variable at the time of declaration, often for classes or Android views.\{\{nl\}\} \{\{fa-exclamation-triangle\}\} Must be a var (NOT val) \{\{nl\}\} \{\{fa-exclamation-triangle\}\} Must be a non-primitive type (no Int, Double, etc.) \{\{nl\}\} \{\{fa-exclamation-triangle\}\} Must be initialized before use\{\{nl\}\}\{\{nl\}\} \{\{fa-code\}\} {\bf{Basic Syntax}} \newline % Row Count 9 (+ 9) `lateinit var variableName: Type` \{\{nl\}\} \{\{fa-code\}\} `lateinit var count: Int` // ❌ Compile-time error \{\{nl\}\} \{\{fa-code\}\} `lateinit var count: String` // ✅ Valid% Row Count 13 (+ 4) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Nullable types}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} A nullable type means a variable can hold a null value. \{\{nl\}\} \{\{fa-chevron-up\}\} Enhances null safety, reducing runtime crashes due to NullPointerException (NPE) from Java.\{\{nl\}\} \{\{fa-exclamation-triangle\}\} Must explicitly allow a variable to be null by adding {\bf{?}} to its type.\{\{nl\}\} \{\{nl\}\} \{\{fa-code\}\} `var studentName: String? = "Sarah" ` \{\{nl\}\} `studentName = null` // ✅ Allowed because of the "?" in String? \{\{nl\}\} \{\{fa-code\}\} `var teacherName: String = "John" `\{\{nl\}\}`teacherName = null` // ❌ Error: Null can not be a value of a ~ nonnull type String% Row Count 12 (+ 12) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Safe Call}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} {\bf{The Safe Call Operator ?.}} allows you to {\bf{safely access}} properties or methods {\bf{only if the variable is NOT null}}. \{\{nl\}\}\{\{fa-chevron-up\}\} If the variable is {\bf{null}}, the call{\bf{ returns null}} instead of throwing a crash. \{\{nl\}\}\{\{nl\}\} \{\{fa-code\}\} {\bf{Basic Syntax}} \newline % Row Count 6 (+ 6) ` \seqsplit{nullableVariable?.methodOrProperty} ` \{\{nl\}\}\{\{fa-code\}\} `var name: String? = "Sarah"` \{\{nl\}\} `println(name?.length)` // Output: 5 \{\{nl\}\} `name = null` \{\{nl\}\}`println(name?.length)` // Output: null (no crash!) \{\{nl\}\}\{\{fa-code\}\} \seqsplit{`student?.school?.address?.city`} // Chain safe calls. Each ?. checks at every level safely% Row Count 13 (+ 7) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Elvis operator}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} {\bf{The Elvis Operator ?: }} is used to {\bf{provide a default value}} when an expression on the left is {\bf{null}}. \{\{nl\}\} \{\{fa-chevron-up\}\} If the left side is {\bf{not null}}, it returns the left side.\{\{nl\}\} \{\{fa-chevron-up\}\} If the left side is {\bf{null}}, it returns the right side (the default). \{\{nl\}\}\{\{nl\}\} \{\{fa-code\}\} {\bf{Basic Syntax}} \newline % Row Count 8 (+ 8) `val result = nullableVariable ?: defaultValue`\{\{nl\}\} \{\{fa-code\}\} `var name: String? = "Sarah"` \{\{nl\}\} `val finalName = name ?: "Unknown" ` \{\{nl\}\} `println(finalName)` // Output: Sarah \{\{nl\}\}\{\{fa-code\}\} `var name: String? = null `\{\{nl\}\} `val finalName = name ?: "Unknown"` \{\{nl\}\} `println(finalName) `// Output: Unknown% Row Count 15 (+ 7) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Classes}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} A {\bf{class}} is a {\bf{blueprint}} for creating objects (instances) with {\bf{properties}} and {\bf{functions}} (methods). \newline % Row Count 3 (+ 3) \{\{nl\}\} \{\{fa-code\}\} {\bf{Basic Syntax}} \newline % Row Count 4 (+ 1) `class User (` \newline % Row Count 5 (+ 1) `var firstName: String,` \newline % Row Count 6 (+ 1) `var lastName: String,` \newline % Row Count 7 (+ 1) `var address: String? = null` \newline % Row Count 8 (+ 1) `)` \newline % Row Count 9 (+ 1) \{\{fa-code\}\} `class Car(val brand: String, var speed: Int) \{` \newline % Row Count 11 (+ 2) `fun drive() \{` \newline % Row Count 12 (+ 1) ` println("Driving \$brand at \$speed km/h")` \newline % Row Count 14 (+ 2) ` \}` \newline % Row Count 15 (+ 1) `\}` \newline % Row Count 16 (+ 1) \{\{fa-code\}\} `val myCar = Car("Toyota", 120)` \newline % Row Count 17 (+ 1) `myCar.drive()` // Output: Driving Toyota at 120 km/h% Row Count 19 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data classes}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} A {\bf{Data Class}} is a special class designed to {\bf{hold data}}. \newline % Row Count 2 (+ 2) \{\{fa-chevron-up\}\} To declare a data class, use the keyword {\bf{data}}. \newline % Row Count 4 (+ 2) \{\{fa-chevron-up\}\} Kotlin {\bf{automatically generates}} useful {\bf{methods}} for you: toString() \newline % Row Count 6 (+ 2) equals(), hashCode(), copy()... \newline % Row Count 7 (+ 1) \{\{nl\}\} \{\{fa-code\}\} {\bf{Basic Syntax}} \newline % Row Count 8 (+ 1) `data class ClassName(val prop1: Type, val prop2: Type)` \newline % Row Count 10 (+ 2) \{\{fa-code\}\} `data class User(val name: String, val age: Int)` \newline % Row Count 12 (+ 2) `val u = User("Sarah", 25)` \newline % Row Count 13 (+ 1) `println(u)` // Output: User(name=Sarah, age=25) \newline % Row Count 14 (+ 1) `val u2 = u.copy(age = 26)` \newline % Row Count 15 (+ 1) `println(u2)` \newline % Row Count 16 (+ 1) // Output: User(name=Sarah, age=26)% Row Count 17 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-exclamation-triangle\}\} Must have at least one property inside primary constructor. \newline \{\{fa-exclamation-triangle\}\} Properties should be val or var Otherwise not allowed.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Collections}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} {\bf{Groups of related elements}} you can store, manage, and manipulate together. \{\{nl\}\} \{\{fa-chevron-circle-right\}\} {\bf{List:}} Ordered collection, allows duplicates. {\bf{listOf(1, 2, 2, 3)}}\{\{nl\}\} \{\{fa-chevron-circle-right\}\} {\bf{Set:}} Unordered, unique elements. {\bf{setOf(1, 2, 3)}}\{\{nl\}\} \{\{fa-chevron-circle-right\}\} {\bf{Map:}} Key-value pairs. {\bf{mapOf("name" to "Sarah")}}\{\{nl\}\}\{\{nl\}\} 🟢 {\bf{Mutable Collections:}} Collections that allow modifications (add/remove/update) after creation. Useful when the data structure needs to change dynamically.\{\{nl\}\} ✅ {\bf{mutableListOf, mutableMapOf, mutableSetOf()}}\{\{nl\}\}\{\{nl\}\} \{\{fa-code\}\} {\bf{Mutable list with explicit type declaration}}\{\{nl\}\} `val shapes: MutableList\textless{}String\textgreater{} = mutableListOf("tri", "squ")`\{\{nl\}\} `shapes.add("cir")`\{\{nl\}\} `println(shapes)` // {[}tri, squ, cir{]}\{\{nl\}\}\{\{nl\}\} 🔴 {\bf{Immutable Collections:}} Immutable collections that cannot be modified after creation. They provide safety by ensuring your data remains unchanged.\{\{nl\}\} ⭕ {\bf{listOf, mapOf, emptyListOf, emptyMapOf}}\{\{nl\}\}\{\{nl\}\} \{\{fa-code\}\} {\bf{Read-only list}}\{\{nl\}\} `val readOnlyShapes = listOf("triangle", "square")`\{\{nl\}\} \seqsplit{`println(readOnlyShapes)`} // {[}triangle, square{]}\{\{nl\}\} `println("First item is: \$\{readOnlyShapes{[}0{]}\}")` // triangle \{\{nl\}\} \{\{nl\}\}% Row Count 26 (+ 26) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.88 cm} x{5.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Common Collection Operations}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Add}} & fruits.add("Mango") \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{Remove}} & fruits.remove("Banana") \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} {\bf{Loop List}} & for (item in list) \{\} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} {\bf{Loop Map}} & for ((k,v) in map) \{\} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} {\bf{Contains}} & list.contains(2) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} {\bf{Size}} & list.size \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Conditional expressions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} In Kotlin, many control structures like if, when are {\bf{expressions}}, {\bf{they return a value}} (not just perform actions like in Java or C++). \newline % Row Count 4 (+ 4) \{\{fa-chevron-up\}\}You can assign the result of an if or when directly to a variable! \newline % Row Count 6 (+ 2) \{\{fa-arrow-circle-right\}\} {\bf{if :}} \newline % Row Count 7 (+ 1) \{\{fa-code\}\} `val max = if (a \textgreater{} b) a else b` // if (a \textgreater{} b) returns a, otherwise b, and assigns the result to max. No need to create extra variables manually. \newline % Row Count 11 (+ 4) There is {\bf{no ternary operator}} condition ? then : else in Kotlin. \newline % Row Count 13 (+ 2) \{\{fa-arrow-circle-right\}\} {\bf{when :}} \newline % Row Count 14 (+ 1) \{\{fa-code\}\} `val result = when (score) \{` \newline % Row Count 15 (+ 1) ` in 90..100 -\textgreater{} "Excellent"` \newline % Row Count 16 (+ 1) ` in 75..89 -\textgreater{} "Good"` \newline % Row Count 17 (+ 1) ` in 60..74 -\textgreater{} "Pass"` \newline % Row Count 18 (+ 1) ` else -\textgreater{} "Fail"` \newline % Row Count 19 (+ 1) \} // All branch conditions are checked sequentially until one of them is satisfied. So only the first suitable branch is executed.% Row Count 22 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Loops}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-circle-right\}\} {\bf{for :}} Iterate over a range of values,array, list, etc and perform an action. \newline % Row Count 3 (+ 3) \{\{fa-code\}\} {\bf{Iterating a list}} \newline % Row Count 4 (+ 1) `val fruits = listOf("Apple", "Banana", "Cherry")` \newline % Row Count 6 (+ 2) `for (fruit in fruits) \{` \newline % Row Count 7 (+ 1) ` println(fruit) \}` \newline % Row Count 8 (+ 1) \{\{fa-code\}\} {\bf{Iterating a range}} \newline % Row Count 9 (+ 1) `for (i in 1..5) \{` \newline % Row Count 10 (+ 1) `println(i)` // Prints 1 2 3 4 5`\}` \newline % Row Count 11 (+ 1) \{\{fa-chevron-circle-right\}\} {\bf{While :}} To execute a code block while a conditional expression is true. \newline % Row Count 14 (+ 3) \{\{fa-code\}\} `var x = 5` \newline % Row Count 15 (+ 1) `while (x \textgreater{} 0) \{` \newline % Row Count 16 (+ 1) `println(x)` \newline % Row Count 17 (+ 1) ` x-{}- \}` // Repeats while x \textgreater{} 0. \newline % Row Count 18 (+ 1) \{\{fa-chevron-circle-right\}\} {\bf{do...while :}} To execute the code block first and then check the conditional expression. \newline % Row Count 21 (+ 3) \{\{fa-code\}\} `var y = 0` \newline % Row Count 22 (+ 1) `do \{` \newline % Row Count 23 (+ 1) `println(y)` \newline % Row Count 24 (+ 1) `y++` \newline % Row Count 25 (+ 1) `\} while (y \textless{} 3)` // Runs at least once, even if condition is false after first execution.% Row Count 27 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Functions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} A function groups reusable code. \newline % Row Count 2 (+ 2) \{\{fa-chevron-up\}\} Can be defined in a class (method) or directly in a package. \newline % Row Count 4 (+ 2) \{\{fa-chevron-up\}\} You can declare your own functions in Kotlin using {\bf{fun}} keyword. \newline % Row Count 6 (+ 2) \{\{nl\}\} \{\{fa-code\}\} Basic Syntax \newline % Row Count 7 (+ 1) `fun functionName(param1: Type, param2: Type): ReturnType` \newline % Row Count 9 (+ 2) `\{` \newline % Row Count 10 (+ 1) // function body \newline % Row Count 11 (+ 1) `\} ` \newline % Row Count 12 (+ 1) \{\{fa-code\}\} `fun add(a: Int, b: Int): Int \{` \newline % Row Count 13 (+ 1) `return a + b` \newline % Row Count 14 (+ 1) `\}` // add(2, 3) returns 5. \newline % Row Count 15 (+ 1) \{\{fa-code\}\} `fun multiply(a: Int, b: Int) = a * b` \newline % Row Count 17 (+ 2) // No \{\} or return needed if it's one expression.% Row Count 18 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Lambda expressions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-chevron-up\}\} A {\bf{Lambda}} is an {\bf{anonymous function}}, a "function literal". \newline % Row Count 2 (+ 2) \{\{fa-chevron-up\}\} A function that is not declared, but passed immediately as an expression. \newline % Row Count 4 (+ 2) \{\{fa-chevron-up\}\} {\bf{Lambda expressions}} can be assigned to variables, passed as \newline % Row Count 6 (+ 2) arguments, or returned from functions. \newline % Row Count 7 (+ 1) \{\{nl\}\}\{\{fa-code\}\} {\bf{Basic Syntax}} \newline % Row Count 8 (+ 1) \{ `parameter1: Type, parameter2: Type -\textgreater{} body `\} \newline % Row Count 9 (+ 1) // \{\} : Lambda block \newline % Row Count 10 (+ 1) // -\textgreater{} : Separates parameters from body \newline % Row Count 11 (+ 1) \{\{fa-code\}\} {\bf{Simple Lambda}} \newline % Row Count 12 (+ 1) `val greet = \{ println("Hello, World!") \}` \newline % Row Count 13 (+ 1) `greet()` // Prints "Hello, World!" \newline % Row Count 14 (+ 1) \{\{fa-code\}\} {\bf{Lambda with parameters}} \newline % Row Count 15 (+ 1) `val add = \{ a: Int, b: Int -\textgreater{} a + b \}` \newline % Row Count 16 (+ 1) `println(add(3, 5))` // Output: 8 \newline % Row Count 17 (+ 1) \{\{fa-code\}\} {\bf{Lambda passed to a function}} \newline % Row Count 18 (+ 1) `fun calc(a: Int, b: Int, op: (Int, Int) -\textgreater{} Int): Int \{` \newline % Row Count 20 (+ 2) `return op(a, b)` \newline % Row Count 21 (+ 1) \}` \newline % Row Count 22 (+ 1) `val res = calc(2, 3) \{ x, y -\textgreater{} x * y \}; println(res)` \newline % Row Count 24 (+ 2) // 6% Row Count 25 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{it keyword :}} When there's only one parameter, it is used automatically. \newline \{\{fa-code\}\} `val square: (Int) -\textgreater{} Int = \{ it * it \} ` \newline `println(square(4))` // Output: 16. No need to name the parameter explicitly if there's only one.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}