What’s important is not having each and every bit of syntax memorized, so feel free to use this a lot, print it out, bookmark it etc. It’s also a good idea to inspire from this and/or make your own as a reference.

  1. Kotlin Cheat Sheet Pdf
  2. Kotlin Syntax Cheat Sheet Pdf
  3. Kotlin With
  4. Vim Kotlin Syntax
  5. Kotlin Programming
  6. Kotlin Syntax Cheat Sheet Printable

'I have participated in great Kotlin for Android Developers training, with extremely competent Marcin Moskala from Kt. Academy as a trainer. It was an intensive 3-days Kotlin workshop, ended with a quite difficult and thorough exam.

Hello World

Kotlin is a modern and powerful language, but getting started with it can be a challenge. When you’re getting going it can be difficult remembering the syntax for common operations, so we’ve put together a downloadable PDF Kotlin cheat sheet and quick reference. In this case I find it handy to create a quick cheat sheet or Coles/Cliff notes version as I learn that programming language. This is that sheet, so if you already know a couple programming languages and want to learn Kotlin, hopefully this will be as useful as any tutorial at getting you going quickly. Title: raywenderlich.com Kotlin Cheat Sheet and Quick Reference Author: raywenderlich.com Created Date: 7/30/2018 8:24:23 PM. DRAFT: algorithms and datastructures java Cheat Sheet. Exam preparation. Java, data, computer, easy, structures and 2 more. 1 Page (0) DRAFT: Java Intro Software Cheat Sheet. Kotlin and Java Syntax Side by Side. Java, syntax, kotlin. 1 Page (0) DRAFT: javaa Cheat Sheet.

The most basic program. Every program starts executing from main().

Variables

These are when you want to store data that you will expect changes more than once.

Type inference is fairly smart so almost all the time you can not mention the type and it will figure it out so put it in place where it matters.

Values

When you want to store something that will not change more than once. If it does use var.

Generally you’d want to use val as much as possible over var.

Types

There are many different built in types for Data in Kotlin. The most important ones are

  • Int - whole numbers 0, 324123, 42, -43, 4234234

  • Double - numbers with decimal/fractional parts allowed 0, 42.125, -4236.3, -0.11233332

  • String - any type of text - “Random text”, “Insurance rates go up 8%”, “42.0”, “25”

  • Boolean - can be either true or false. Useful in control flow like if, when, for, while etc.

Converting

Generally it’s to<Type>() like toInt(), toDouble(), toString()

Basic math

Most standard operations work with numeric types like Int and Double

Note that if we want normal division over integer division we have to convert one of them to a Double or some decimal type.

Printing - console output

Basically use print() to display, println() to print with a new line and string interpolation like${<variable name>} inside to print out values.

Most of the time if it’s just a single variable name the {} are not requried and it’s just $<variable name>

If

Note as a block else is optional but when it’s being used as an expression (ex. the quickGrade assignment above) then else has to be there.

If can be also used well with collections

as well as ranges

When

For when there are more than two branches possible. This has some very powerful syntax better than C-like languages like switch

It works with single values, several values collected, ranges and collections as well as many more complex matchers.

Repeat

While

For

For looping over anything that is Iterable like ranges, collections etc.

Ranges

Arrays

Lists

Maps

Here it’s slightly different because we can assign two variables, one for the key and the other for the value. Note the bracket around the variable part since we have more than one.

Functions

A simple function with no parameters and no return

Unit indicated that there is no return, since it’s optional we usually write it like

Parameters/Arguments

Seperated by commas like functionName(name1: Type1, name2: Type2)

For variable number of arguments (Varargs) we use the vararg keyword

Return

return brings the control back to the function that called it.

If you add a type then it gives back that value, like an output.

Parameterized arguments and return

Also called Generics and Parametric polymorphism

For parameterized types we want to write general functions over writing the same one again and again. Also called parametric polymorphism. We use <T> that denotes any generic type.

Lambdas

Lambdas are like functions which can be assigned to variables, passed around functions and returned. It basically has a type (<list of types>) -><return type>

Kotlin is quite smart with type inference so this can also be

Higher order functions

Lambdas can be used in higher order functions (functions that take in other functions as input)

Making one ourselves

Classes and Objects

Classes wrap properties and behaviour. We use the class keyword

val makes it read only, var means it will change. The variables can be accessed within the functions as is and other objects or outside the class you need the . dot notation. You also need the . notation to call functions outside the class.

Excel

To use objects of the class:

Data Classes

The data keyword before the class name sets up the standard utility functions (Plain Old Java Object) with ain class.

Lets us do:

Kotlin Cheat Sheet Pdf

Inheritance

Null types

One of the neatest things about Kotlin comes from it’s null safe programming idioms. Unless you specifically allow Kotlin to, you will never encounter a NullPointerException as null checking becomes the compiler’s job.

For this we add ? at the end of the variable type to say, this can be of the type or null. This applies to function parameters, variable names, returns, class instance variables.

Or here is an example with functions

Null safe calling

Kotlin also provides idiomatic ways to deal with functions that may return null. You’ll often see this kind of code when interfacing with Java or Android libraries especially.

Reference

To call a method on something that results in a nullable type (ending with ?) instead of a normal method call . we use ?. which acts normally when the data is not null and when the data is null it just returns null instead of trying to call a function on null and creating a null pointer exception. The Kotlin compiler will make sure things propogate through the chain.

The Elvis operator ?: can be used at the end of null chain calls when you want a default value in case you ended up with null.

Console input

readLine() gives us a line of input as a String, the ?. null safe calling takes care of input issues

Coroutines

What’s important is not having each and every bit of syntax memorized, so feel free to use this a lot, print it out, bookmark it etc. It’s also a good idea to inspire from this and/or make your own as a reference.

Hello World

The most basic program. Every program starts executing from main().

Variables

These are when you want to store data that you will expect changes more than once.

Type inference is fairly smart so almost all the time you can not mention the type and it will figure it out so put it in place where it matters.

Values

When you want to store something that will not change more than once. If it does use var.

Generally you’d want to use val as much as possible over var.

Types

There are many different built in types for Data in Kotlin. The most important ones are

  • Int - whole numbers 0, 324123, 42, -43, 4234234

  • Double - numbers with decimal/fractional parts allowed 0, 42.125, -4236.3, -0.11233332

  • String - any type of text - “Random text”, “Insurance rates go up 8%”, “42.0”, “25”

  • Boolean - can be either true or false. Useful in control flow like if, when, for, while etc.

Converting

Generally it’s to<Type>() like toInt(), toDouble(), toString()

Basic math

Most standard operations work with numeric types like Int and Double

Note that if we want normal division over integer division we have to convert one of them to a Double or some decimal type.

Printing - console output

Basically use print() to display, println() to print with a new line and string interpolation like${<variable name>} inside to print out values.

Most of the time if it’s just a single variable name the {} are not requried and it’s just $<variable name>

If

Note as a block else is optional but when it’s being used as an expression (ex. the quickGrade assignment above) then else has to be there.

If can be also used well with collections

as well as ranges

When

For when there are more than two branches possible. This has some very powerful syntax better than C-like languages like switch

It works with single values, several values collected, ranges and collections as well as many more complex matchers.

Repeat

While

For

For looping over anything that is Iterable like ranges, collections etc.

Ranges

Arrays

Lists

Maps

Here it’s slightly different because we can assign two variables, one for the key and the other for the value. Note the bracket around the variable part since we have more than one.

Functions

A simple function with no parameters and no return

Unit indicated that there is no return, since it’s optional we usually write it like

Parameters/Arguments

Seperated by commas like functionName(name1: Type1, name2: Type2)

For variable number of arguments (Varargs) we use the vararg keyword

Kotlin Syntax Cheat Sheet Pdf

Return

return brings the control back to the function that called it.

If you add a type then it gives back that value, like an output.

Parameterized arguments and return

Also called Generics and Parametric polymorphism

For parameterized types we want to write general functions over writing the same one again and again. Also called parametric polymorphism. We use <T> that denotes any generic type.

Lambdas

Lambdas are like functions which can be assigned to variables, passed around functions and returned. It basically has a type (<list of types>) -><return type>

Kotlin is quite smart with type inference so this can also be

Higher order functions

Lambdas can be used in higher order functions (functions that take in other functions as input)

Making one ourselves

Classes and Objects

Classes wrap properties and behaviour. We use the class keyword

val makes it read only, var means it will change. The variables can be accessed within the functions as is and other objects or outside the class you need the . dot notation. You also need the . notation to call functions outside the class.

To use objects of the class:

Data Classes

The data keyword before the class name sets up the standard utility functions (Plain Old Java Object) with ain class.

Kotlin With

Lets us do:

Inheritance

Null types

One of the neatest things about Kotlin comes from it’s null safe programming idioms. Unless you specifically allow Kotlin to, you will never encounter a NullPointerException as null checking becomes the compiler’s job.

Vim Kotlin Syntax

For this we add ? at the end of the variable type to say, this can be of the type or null. This applies to function parameters, variable names, returns, class instance variables.

Or here is an example with functions

Null safe calling

Kotlin Programming

Kotlin also provides idiomatic ways to deal with functions that may return null. You’ll often see this kind of code when interfacing with Java or Android libraries especially.

To call a method on something that results in a nullable type (ending with ?) instead of a normal method call . we use ?. which acts normally when the data is not null and when the data is null it just returns null instead of trying to call a function on null and creating a null pointer exception. The Kotlin compiler will make sure things propogate through the chain.

The Elvis operator ?: can be used at the end of null chain calls when you want a default value in case you ended up with null.

Console input

Kotlin Syntax Cheat Sheet Printable

readLine() gives us a line of input as a String, the ?. null safe calling takes care of input issues

Coroutines