Skip to content
Justice Tech School

Kotlin Tutorial

A concise, null safe language for Android and servers.

Start learning Kotlin
Getting started3 lessons
  1. 1Kotlin IntroductionKotlin is a modern language from JetBrains that runs on the JVM.
  2. 2Kotlin Get StartedUse IntelliJ IDEA or Android Studio, or try code in the online Kotlin Playground.
  3. 3Kotlin SyntaxSemicolons are optional.
Basics25 lessons
  1. 4Kotlin Variablesval creates a read only variable and var creates one you can change.
  2. 5Kotlin Data TypesNumbers are Int, Long, Double and Float.
  3. 6Kotlin StringsStrings use $name or ${expression} templates.
  4. 7Kotlin Null SafetyTypes are non null by default.
  5. 8Kotlin Input and Outputprintln prints with a newline and print does not.
  6. 9Kotlin CommentsUse // for one line and /* */ for several.
  7. 10Kotlin BooleansBoolean has two values, true and false.
  8. 11Kotlin Constantsconst val declares a compile time constant at top level or in an object.
  9. 12Type Checks and Smart Castsis tests a type, and after the test Kotlin smart casts the value automatically.
  10. 13Kotlin Break and Continuebreak exits a loop and continue skips to the next round.
  11. 14val and varval is read only and var can change.
  12. 15Basic TypesInt, Long, Double, Float, Boolean, Char and String.
  13. 16Null SafetyTypes cannot hold null unless you add ?.
  14. 17String Templates$name and ${expression} insert values into strings.
  15. 18Type ConversionKotlin never converts numbers implicitly.
  16. 19OperatorsArithmetic, comparison, logical and range operators.
  17. 20Equality: == and ===== compares values with equals and === compares references.
  18. 21Reading Inputreadln() reads a line and readlnOrNull() returns null at the end of input.
  19. 22Math Functionskotlin.math has abs, max, min, pow, sqrt, round, floor, ceil and PI.
  20. 23Random Numberskotlin.random.Random gives ints, doubles and booleans.
  21. 24The main FunctionEvery program starts in main.
  22. 25Comments and KDoc// starts a line comment and /* */ a block.
  23. 26const and lateinitconst val is a compile time constant.
  24. 27Char and BooleanChar holds one character and supports arithmetic.
  25. 28Any, is and asAny is the root type.
Operators2 lessons
  1. 29Kotlin OperatorsArithmetic: + - * / %.
  2. 30Kotlin RangesRanges use .
Control flow9 lessons
  1. 31Kotlin If as an Expressionif is an expression, so it returns a value and replaces the ternary operator.
  2. 32Kotlin Whenwhen replaces switch and is much more powerful.
  3. 33Kotlin Loopsfor iterates over ranges and collections, while and do while repeat on a condition.
  4. 34if as an Expressionif returns a value, so there is no ternary operator.
  5. 35whenwhen replaces switch.
  6. 36for Loopsfor walks any iterable.
  7. 37while and do whilewhile checks first and do while runs at least once.
  8. 38break, continue and Labelsbreak leaves a loop, continue skips a round and labels target outer loops.
  9. 39Ranges and ProgressionsA range such as 1..5 is a sequence you can test, loop over and convert.
Collections18 lessons
  1. 40Kotlin ArraysarrayOf creates an array of fixed size.
  2. 41Kotlin ListslistOf creates an immutable list and mutableListOf one you can change.
  3. 42Kotlin Sets and MapsA set holds unique values and a map holds key value pairs.
  4. 43Kotlin Collection Functionsmap, filter, reduce, sum, any, all, groupBy and sortedBy transform collections in a functi.
  5. 44ListslistOf is read only and mutableListOf can change.
  6. 45Collection Operationsmap, filter, reduce, sortedBy, groupBy, any, all, sum and take are the daily tools.
  7. 46SetssetOf holds unique values.
  8. 47MapsmapOf pairs keys with values using to.
  9. 48SequencesSequences are lazy.
  10. 49ArraysarrayOf and intArrayOf create fixed size arrays.
  11. 50DestructuringPull values from pairs, data classes and maps into variables.
  12. 51String OperationsCommon string functions: substring, split, replace, trim, startsWith, contains and format.
  13. 52groupBy, associate and partitiongroupBy buckets items, associateBy builds a map and partition splits in two.
  14. 53fold and reducereduce combines items with the first as the start.
  15. 54zip, flatten and windowedzip pairs two lists, flatten merges nested lists and windowed makes sliding windows.
  16. 55distinct, count and minOfdistinct removes duplicates, count counts matches and minBy and maxBy find extremes.
  17. 56StringBuilder and buildStringUse StringBuilder or buildString for many appends.
  18. 57Pairs and TriplesPair and Triple hold two or three values.
Functions10 lessons
  1. 58Kotlin FunctionsDeclare with fun.
  2. 59Default and Named ArgumentsParameters can have defaults, and callers can name arguments in any order.
  3. 60Lambdas and Higher Order FunctionsA lambda is a function written in braces.
  4. 61Functionsfun declares a function.
  5. 62Default and Named ArgumentsGive parameters defaults and name them when calling.
  6. 63varargvararg accepts any number of arguments as an array.
  7. 64Lambdas and Higher Order FunctionsA lambda is { params -> body }.
  8. 65Extension FunctionsAdd functions to existing types without inheritance.
  9. 66Scope Functionslet, run, apply, also and with run code on an object.
  10. 67Recursion and tailrecA function may call itself.
Object oriented Kotlin21 lessons
  1. 68Kotlin Classes and ObjectsA class has properties and functions.
  2. 69Kotlin InheritanceClasses are final by default, so mark a base class open.
  3. 70Kotlin Data ClassesA data class automatically gets equals, hashCode, toString and copy.
  4. 71Kotlin Interfaces and Abstract ClassesAn interface lists members and may give default implementations.
  5. 72Object and Companion Objectobject declares a singleton.
  6. 73Enums and Sealed Classesenum class defines constants.
  7. 74Properties, Getters and SettersProperties can have custom get and set blocks.
  8. 75Visibility Modifierspublic is the default.
  9. 76Operator OverloadingMark a function operator to give + - * and others meaning for your own types, for example .
  10. 77Destructuring and TypealiasDestructuring unpacks a data class or pair into variables.
  11. 78ClassesA class has a primary constructor, properties and methods.
  12. 79Data Classesdata classes generate equals, hashCode, toString, copy and componentN.
  13. 80Properties, Getters and SettersProperties have automatic getters and setters.
  14. 81InheritanceClasses are final by default.
  15. 82InterfacesInterfaces can contain default method bodies.
  16. 83abstract and sealed Classesabstract classes cannot be created.
  17. 84object and companion objectobject makes a singleton.
  18. 85Enum Classesenum class lists constants that can have properties and methods.
  19. 86GenericsType parameters make classes and functions work with many types.
  20. 87Delegation and lazyby lazy runs code once on first use.
  21. 88Operator Overloadingoperator fun plus, minus, get and compareTo make types work with operators.
Errors and files11 lessons
  1. 89Kotlin Exceptionstry, catch and finally handle errors.
  2. 90Kotlin Filesjava.io.File with Kotlin extensions reads and writes: readText, writeText, appendText, rea.
  3. 91try, catch and finallytry is an expression.
  4. 92throw and Custom Exceptionsthrow raises an exception.
  5. 93FilesreadText, writeText and forEachLine handle files.
  6. 94Coroutines IntroductionCoroutines run asynchronous work in a sequential style.
  7. 95Safe Calls and let?.let runs a block only if the value is not null.
  8. 96Smart CastsAfter an is check the compiler treats the value as that type.
  9. 97Practice: Word FrequencySplit, group and sort.
  10. 98Practice: FizzBuzzCombine a range with when.
  11. 99Practice: Bank AccountCombine a class, validation and exceptions.
Advanced KotlinPremium7 lessons
  1. 100Extension FunctionsAdd functions to existing types without inheriting from them.
  2. 101Scope Functionslet, run, with, apply and also run a block on an object.
  3. 102Kotlin GenericsGenerics make code work with many types safely.
  4. 103CoroutinesCoroutines run asynchronous work without blocking threads.
  5. 104Kotlin FlowA Flow emits values over time and is the coroutine version of sequences or streams.
  6. 105Delegation and Lazyby delegates behaviour.
  7. 106Inline Functions and DSLsinline copies a function body into call sites to avoid lambda overhead.
Kotlin in practicePremium6 lessons
  1. 107Kotlin for AndroidAndroid apps use Kotlin with Jetpack Compose for the UI.
  2. 108Ktor Web ServerKtor is a Kotlin framework for servers and clients.
  3. 109Testing KotlinUse JUnit with kotlin.test.
  4. 110Gradle and DependenciesGradle builds Kotlin projects.
  5. 111Java InteroperabilityKotlin and Java mix in one project.
  6. 112Kotlin Best PracticesPrefer val, immutable collections and data classes.
Next steps1 lessons
  1. 113What Next?Build a small Android app with Jetpack Compose, then a server with Ktor.