Leanpub Header

Skip to main content

Kotlin Essentials

This book covers the essentials of Kotlin so you can start developing with this amazing programming language. We show nearly everything you need to know in clear and executable code examples.

The author is letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
About

About

About the Book

Kotlin is a powerful language, largely thanks to its expressive syntax, intuitive and null-safe type system, and great tooling support. In this book, we cover the essentials of Kotlin, so you can start developing with this amazing programming language. We show nearly everything you need to know in clear and executable code examples.

If you are interested in paperback, you can purchase it here.

Share this book

Categories

Price

Pick Your Price...

Minimum price

$19.00

$29.00

You pay

$29.00

Author earns

$23.20
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 2)

2
The author will earn $24.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Author

Marcin Moskała

Marcin Moskala is an experienced developer and Kotlin trainer. He is the founder of the kt.academy, Kotlin GDE, an official JetBrains partner for teaching Kotlin, and author of the books Effective Kotlin, Kotlin Coroutines, and Android Development with Kotlin.

Leanpub Podcast

Episode 254

An Interview with Marcin Moskała

Translations

Translations

Languages

Contents

Table of Contents

Introduction

  1. Who is this book for?
  2. What will be covered?
  3. The Kotlin for Developers series
  4. My story
  5. Conventions
  6. Code conventions
  7. Exercises and solutions
  8. Acknowledgments

What is Kotlin?

  1. Kotlin platforms
  2. The Kotlin IDE
  3. Where do we use Kotlin?

Your first program in Kotlin

  1. Live templates
  2. What is under the hood on JVM?
  3. Packages and importing
  4. Summary
  5. Exercise: Your first program

Variables

Basic types, their literals and operations

  1. Numbers
  2. Booleans
  3. Characters
  4. Strings
  5. Summary
  6. Exercise: Basic values operations

Conditional statements

  1. if-statement
  2. when-statement
  3. when-statement with a value
  4. is check
  5. Explicit casting
  6. Smart-casting
  7. While and do-while statements
  8. Summary
  9. Exercise: Using when
  10. Exercise: Pretty time display

Functions

  1. Single-expression functions
  2. Functions on all levels
  3. Parameters and arguments
  4. Unit return type
  5. Vararg parameters
  6. Named parameter syntax and default arguments
  7. Function overloading
  8. Infix syntax
  9. Function formatting
  10. Summary
  11. Exercise: Person details display

The power of the for-loop

  1. Ranges
  2. Break and continue
  3. Use cases
  4. Summary
  5. Exercise: Range Operations

Nullability

  1. Safe calls
  2. Not-null assertion
  3. Smart-casting
  4. The Elvis operator
  5. Extensions on nullable types
  6. null is our friend
  7. lateinit
  8. Summary
  9. Exercise: User Information Processor

Classes

  1. Member functions
  2. Properties
  3. Constructors
  4. Classes representing data in Kotlin and Java
  5. Inner classes
  6. Summary
  7. Exercise: Implementing the Product class

Inheritance

  1. Overriding elements
  2. Parents with non-empty constructors
  3. Super call
  4. Abstract class
  5. Interfaces
  6. Visibility
  7. Any
  8. Summary
  9. Exercise: GUI View Hierarchy Simulation

Data classes

  1. Transforming to a string
  2. Objects equality
  3. Hash code
  4. Copying objects
  5. Destructuring
  6. When and how should we use destructuring?
  7. Data class limitations
  8. Prefer data classes instead of tuples
  9. Summary
  10. Exercise: Data class practice

Objects

  1. Object expressions
  2. Object declaration
  3. Companion objects
  4. Data object declarations
  5. Constant values
  6. Summary
  7. Exercise: Pizza factory

Exceptions

  1. Throwing exceptions
  2. Defining exceptions
  3. Catching exceptions
  4. A try-catch block used as an expression
  5. The finally block
  6. Important exceptions
  7. The hierarchy of exceptions
  8. Summary
  9. Exercise: Catching exceptions

Enum classes

  1. Data in enum values
  2. Enum classes with custom methods
  3. Summary
  4. Exercise: Days of the week enum

Sealed classes and interfaces

  1. Sealed classes and when expressions
  2. Sealed vs enum
  3. Use cases
  4. Summary

Annotation classes

  1. Meta-annotations
  2. Annotating the primary constructor
  3. List literals
  4. Summary

Extensions

  1. Extension functions under the hood
  2. Extension properties
  3. Extensions vs members
  4. Extension functions on object declarations
  5. Member extension functions
  6. Use cases
  7. Summary
  8. Exercise: Conversion and measurement unit creation

Collections

  1. The hierarchy of interfaces
  2. Mutable vs read-only types
  3. Creating collections
  4. Lists
  5. Sets
  6. Maps
  7. Using arrays in practice
  8. Summary
  9. Exercise: Inventory management

Operator overloading

  1. An example of operator overloading
  2. Arithmetic operators
  3. The in operator
  4. The iterator operator
  5. The equality and inequality operators
  6. Comparison operators
  7. The indexed access operator
  8. Augmented assignments
  9. Unary prefix operators
  10. Increment and decrement
  11. The invoke operator
  12. Precedence
  13. Summary
  14. Exercise: Money operations

The beauty of Kotlin’s type system

  1. What is a type?
  2. Why do we have types?
  3. The relation between classes and types
  4. Class vs type in practice
  5. The relationship between types
  6. The subtype of all the types: Nothing
  7. The result type from return and throw
  8. When is some code not reachable?
  9. The type of null
  10. Summary
  11. Exercise: The closest supertype of types

Generics

  1. Generic functions
  2. Generic classes
  3. Generic classes and nullability
  4. Generic interfaces
  5. Type parameters and inheritance
  6. Type erasure
  7. Generic constraints
  8. Star projection
  9. Underscore operator for type arguments
  10. Summary
  11. Exercise: Stock

Final words

  1. Final Project: Workout manager

Exercise solutions

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub