\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{BeckyIndirectlyToo} \pdfinfo{ /Title (kotlin-basics.pdf) /Creator (Cheatography) /Author (BeckyIndirectlyToo) /Subject (Kotlin Basics 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}{80BD84} \definecolor{LightBackground}{HTML}{F7FAF7} \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 Basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{BeckyIndirectlyToo} via \textcolor{DarkBackground}{\uline{cheatography.com/192748/cs/40091/}}} \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}BeckyIndirectlyToo \\ \uline{cheatography.com/beckyindirectlytoo} \\ \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 31st August, 2023.\\ 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{6.8 cm} x{1.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comments}} \tn % Row 0 \SetRowColor{LightBackground} //single-line & \seqsplit{comment} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} /* multiline */ & \seqsplit{comment} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} /* blah \{\{nl\}\} /* nested */ \{\{nl\}\} blah */ & \seqsplit{comment} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{If - Else If - Else Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{val count = 42 \newline if (count == 42) \{ \newline ~ println ("I have the answer") \newline \} else if (count \textgreater{} 35 \&\& count \textless{} 49) \{ \newline ~ printlin ("The answer is close") \newline \} else \{ \newline ~ println ("The answer eludes me.") \newline \}} \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}{When Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{cal answerString = when \{ \newline ~ count == 42 -\textgreater{} "I have the answer" \newline ~ count \textgreater{} 35 -\textgreater{} "The answer is close." \newline ~ else -\textgreater{} "The answer eludes me." \newline \}} \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}{Array Examples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{// {\bf{once an array is created the size is fixed}} \newline val school = arrayOf("shark", "salmon", "minnow", 2) \newline println(java.util.Arrays.toString(school)) \newline \newline //{\bf{mixed type array}} \newline val numbers = intArrayOf(1,2,3) \newline \newline //{\bf{print array}} \newline for (element in school)\{ \newline ~ print(element + " ") \newline \} \newline \newline // {\bf{concatenate arrays}} \newline array3 = array1 + array2 \newline \newline // {\bf{initialize array with code}} \newline val array = Array (5) \{ it * 2\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{elements cannot be added or removed, except by copying to a new array \newline {\bf{can loop through elements and index at the same time}}} \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}{Lists}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{// {\bf{create list, cannot be changed}} \newline val school = listOf("mackerel", "trout") \newline \newline // {\bf{create list, can be changed}} \newline val myList = mutableListOf("tuna'", "salmon") \{\{nl\}\}myList.remove("shark")\{\{nl\}\}} \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 Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{// {\bf{create function}} \newline var count:Int = 42 \newline fun generateAnswerString(): String \{ \newline ~ val answerString = if (count ==42) \{ \newline ~ ~ "I have the answer." \newline ~ \} else \{ \newline ~ ~ "The anwer eludes me" \newline ~ \} \newline ~ return answerString \newline \}} \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}{Anonymous Function Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{//{\bf{Anonymous functions}} \newline val stringLengthFunc: (String) -\textgreater{} Int = \{input -\textgreater{} \newline ~ input.length \newline \} \newline \newline // {\bf{call anonymous function}} \newline val stringLength: Int = \seqsplit{stringLengthFunc("Android")}} \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}{Class Examples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{//{\bf{create class}} \newline class Car\{ \newline ~ //properties \newline ~ val wheels = listOf\textless{}Wheel\textgreater{}() \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.52 cm} x{2.584 cm} x{3.496 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Type}} & {\bf{Size(bits)}} & {\bf{Values}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Byte & 8 & -128 to 127 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} Short & 16 & -32768 to 32767 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} Int & 32 & -2\textasciicircum{}31\textasciicircum{} to 2\textasciicircum{}31\textasciicircum{} -1 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} Long & 64 & -2\textasciicircum{}63\textasciicircum{} to 2\textasciicircum{}63\textasciicircum{} - 1 \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.2 cm} x{4.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variable Declaration}} \tn % Row 0 \SetRowColor{LightBackground} val count : Int = 10 & value can never change \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} var count : Double = 10.01 & value can change \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} val eFloat : Float = 3.14f & needs the f suffex \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} val name: String? = null & add ? to make a nullable value \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} val name : String? = name!! & not-null assertion operator \{\{nl\}\} throws exception if full \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{variable interpolation: \$count \newline {\bf{Kotlin variables can't hold null values by default.}}} \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}{For Loop Examples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{// {\bf{print index and element}} \newline for ((index, element) in school.withIndex())\{ \newline ~ println("Item at \$index is \$element\textbackslash{}n") \newline \} \newline \newline //{\bf{print range of numbers or letters}} \newline for (i in 'a'..'h') print (i) \newline for (i in 5 downTo 1 step 1.5) print(i)} \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}{While Examples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var bubbles = 0 \newline while (bubbles \textless{} 50) \{ \newline ~ bubbles++ \newline \} \newline println("\$bubbles bubbles in the water/n") \newline \newline do \{ \newline ~ bubbles-{}- \newline \} while (bubbles \textgreater{} 50) \newline println("\$bubbles bubbles in the water/n") \newline \newline repeat(2) \{ \newline ~ println("A fish is swimming.") \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Function with Argument Example}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{// {\bf{call function}} \newline val answerString = generateAnswerString() \newline \newline // {\bf{function with argument}} \newline fun generateAnswerString (countThreshold: Int) :String \{ \newline ~ ... \newline ~ \} \newline ~ return answerString \newline \} \newline \newline // {\bf{call function with argument}} \newline \newline val answerString = generateAnswerString(42)} \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}{Void Function Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{// {\bf{void function}} \newline fun printSum(a: Int, b: Int): Unit \{ \newline ~ println("sum of \$a and \$b is \$\{a + b\}") \newline \}} \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}{Higher-Order Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{// {\bf{functions that can take functions as arguments}} \newline fun stringMapper(str:String, mapper: (String) -\textgreater{} Int): Int \{ \newline ~ //Invoke function \newline ~ return mapper(str) \newline \} \newline \newline //**anonymous function can be called outside the parenthesis \newline stringMapper("Android") \{ input -\textgreater{} \newline ~ input.length \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}