Table of Contents
Kotlin Topics
Return to Kotlin, Programming topics or Topics
See Syntax highlighting - https://www.dokuwiki.org/syntax_highlighting
- Snippet from Wikipedia: Kotlin
Kotlin () is a cross-platform, statically typed, general-purpose high-level programming language with type inference. Kotlin is designed to interoperate fully with Java, and the Java virtual machine (JVM) version of Kotlin's standard library depends on the Java Class Library. Type inference allows for more concise syntax than Java, while null-safety features reduce a common class of runtime errors. Kotlin mainly targets the JVM, but also compiles to JavaScript (e.g., for frontend web applications using React) or native code via LLVM (e.g., for native iOS apps sharing business logic with Android apps). JetBrains bears language development costs, while the Kotlin Foundation protects the Kotlin trademark.
On 7 May 2019, Google announced Kotlin had become its preferred language for Android app developers. As of 2024, over 95% of the top 1,000 Android apps on Google Play contain Kotlin code. In May 2024, JetBrains released Kotlin 2.0 featuring the new K2 compiler—a complete rewrite offering up to twice the compilation speed of its predecessor—as well as stabilised support for Kotlin Multiplatform. That same month, Google formally endorsed Kotlin Multiplatform at Google I/O 2024, recommending it as the approach for sharing business logic across Android, iOS, and other platforms.
Table of Contents
-
- Kotlin features - 2.1.1
- Kotlin Quick Reference The REPL - 2.1.4
- Kotlin Quick Reference I/O - 2.1.9
-
- Kotlin Quick Reference for loops - 3.1.2
while, do/while 3.1.3
when (like switch) 3.1.4
try/catch/finally 3.1.5
Classes and Objects 4.1
Constructors and members 4.1.1
Imports and packages 4.1.2
Class members 4.1.3
Constructor default values 4.1.5
Secondary constructors 4.1.6
Open and final classes 4.1.7
Abstract classes 4.1.8
Interfaces 4.1.9
== and === 4.1.10
Enumerations 4.1.11
A complete class 4.1.12
Data classes 4.1.13
Objects 4.1.14
Companion objects 4.1.15
Visibility modifiers 4.1.16
Functions 5.1
Extension functions 5.1.1
Infix functions 5.1.2
Anonymous functions 5.1.3
Passing functions around 5.1.4
vararg parameters 5.1.5
Nullability 6.1
Safe-call operator 6.1.2
Elvis operator 6.1.3
let operator 6.1.4
!! operator 6.1.5
Nullability example 6.1.6
Nullability summary 6.1.7
Collections 7.1
Array 7.1.1
List 7.1.2
Map 7.1.3
Set 7.1.4
Sequence methods 7.1.5
Map methods 7.1.6
Miscellaneous 8.1
A Swing example 8.1.1
Build tools 8.1.2
Idioms 8.1.3
An OOP example 8.1.4
An FP example 8.1.5
Command line 8.1.6
Android 9.1
Contributors 10.1
License 10.2
About 10.3
Introduction
Kotlin Quick Reference is intended to provide a quick reference to the Kotlin programming language. Each chapter in the book demonstrates Kotlin syntax and provides examples to explain the chapter’s topic. This is done in a brisk manner, with as few words as necessary.
Audience
Because this book provides only a quick reference to the Kotlin language, it’s intended for developers who have experience in other programming languages and just need a fast reference to Kotlin features. If you need a more complete book, Kotlin in Action is an excellent resource.
Preface
Release information
This is Release 0.1 of Kotlin Quick Reference.
Since 2011 I’ve been using the Scala programming language. I fell in love with as soon as I saw it, so much so that I wrote three books about it:
Scala Cookbook
Functional Programming, Simplified
Hello, Scala
The only problem I have with Scala is that I enjoy developing Android applications, and with the exception of the Scala on Android project — which hasn’t had a new release since February 24, 2017 — there isn’t a simple way to create Android applications with Scala. So when I looked into Kotlin and saw that it was very similar to Scala, well, I was pretty happy.
What I love about Kotlin
What I love about both Kotlin (and Scala) can be summed up this way:
The syntax is as elegant and concise as Ruby
Everything feels dynamic, but it’s statically typed
Source code compiles to class files that run on the JVM
You can use all of the thousands of Java libraries in existence
Just as I’ve seen with Scala, the Kotlin creators state that as a rough estimate, Kotlin requires about 40% fewer lines of code than Java
Developing Android applications with Kotlin is just as easy (easier!) as creating Android applications with Java
Goal of this book
While there are now many good Kotlin books available, and the documentation on the Kotlin website is excellent, I’ve found that what I want is a quick reference to the Kotlin language. Having used multiple programming languages before — including Scala — I don’t need a lot of discussion about a new language, I mostly just need to see the language’s syntax and some good examples.
Therefore, my goal in this book is to provide a quick reference to the Kotlin language — light on words, and heavy on demonstrating syntax and examples.
S
Kotlin singleton - “A Kotlin object declared with the Kotlin object keyword; singletons are limited to a single instance throughout program execution.” B07FXQ7SQN
