Mar 15, 2019 - We prepared for you Kotlin Cheat sheet, so you can have the most important elements close at hand - it is the best everyday support for Kotlin developer.
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.
- From Java To Kotlin – Your Cheat Sheet For Java To Kotlin, a cross between an overview of the language and an actual cheatsheet. Using Kotlin in an existing Java code base, a talk with theory and code examples on the practical issues of integrating Kotlin with existing Java projects.
- Really quick cheat sheet (more of a cheat card). Remember that a property in Kotlin is effectively just a pair of getter and setter methods. Via the by syntax.
- Jun 26, 2018 - We prepared for you Kotlin Cheat sheet, so you can have the most important elements close at hand - it is the best everyday support for Kotlin developer. Sign up to our newsletter to get the pdf.
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 numbers0
,324123
,42
,-43
,4234234
Double
- numbers with decimal/fractional parts allowed0
,42.125
,-4236.3
,-0.11233332
String
- any type of text - “Random text”, “Insurance rates go up 8%”, “42.0”, “25”Boolean
- can be eithertrue
orfalse
. Useful in control flow likeif
,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
Tutorialspoint Kotlin
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.
Kotlin Syntax Cheat Sheet
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:
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.
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
.
Kotlin Syntax Cheat Sheet Pdf
Console input
readLine()
gives us a line of input as a String
, the ?.
null safe calling takes care of input issues