Kotlin Quick Start Guide
eBook - ePub

Kotlin Quick Start Guide

Core features to get you ready for developing applications

  1. 178 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Kotlin Quick Start Guide

Core features to get you ready for developing applications

Book details
Book preview
Table of contents
Citations

About This Book

Get started with Kotlin programming for building real world applications

Key Features

  • Start programming with Kotlin
  • Explore Kotlin language syntax, standard libraries and Java Interoperability
  • Builds an example application with what you learn

Book Description

Kotlin is a general purpose, object-oriented language that primarily targets the JVM and Android. Intended as a better alternative to Java, its main goals are high interoperability with Java and increased developer productivity. Kotlin is still a new language and this book will help you to learn the core Kotlin features and get you ready for developing applications with Kotlin.

This book covers Kotlin features in detail and explains them with practical code examples.You will learn how to set up the environment and take your frst steps with Kotlin and its syntax. We will cover the basics of the language, including functions, variables, and basic data types. With the basics covered, the next chapters show how functions are first-class citizens in Kotlin and deal with the object-oriented side of Kotlin.

You will move on to more advanced features of Kotlin. You will explore Kotlin's Standard Library and learn how to work with the Collections API. The book finishes by putting Kotlin in to practice, showing how to build a desktop app.

By the end of this book, you will be confident enough to use Kotlin for your next project.

What you will learn

  • Programming in Kotlin language syntax, basic types, control?ow, classes, and OOP
  • Writing functions and functional programming in Kotlin
  • Defning and importing from packages in Kotlin
  • Running Kotlin on JVMs and Android runtimes
  • Working with the Kotlin Standard Library and advanced features of Kotlin programming
  • Setting up a Kotlin development environment with JetBrains tools
  • Building real-world applications with Kotlin

Who this book is for

This book is intended for anybody who wants to learn the most important Kotlin features. No experience of Kotlin is expected.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Kotlin Quick Start Guide by Marko Devcic in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789342598
Edition
1

Classes and Object-Oriented Programming

Kotlin is an object-oriented (OO) language, and classes are the main element of an OO language. This chapter will delve into the details of working with classes in Kotlin.
Even if you have experience with Java or similar languages, you can still learn from this chapter. There are a lot of differences from Java when working with classes, such as the constructor syntax, default visibility modifiers, properties, and so on. Kotlin also has some unique features not found in other languages, such as sealed and data classes.
In this chapter, you will learn about the following:
  • Classes
  • Properties
  • Class constructors
  • Nested classes
  • Enum classes
  • Data classes
  • Inheritance
  • Overriding members
  • Abstract classes
  • Interfaces
  • Interfaces versus abstract classes
  • The object keyword
  • Class delegation
  • Sealed classes
  • Smart casts

Classes

To begin with classes, let's take a look how Java would declare a class, with a couple of fields that are initialized in the class constructor. To enforce encapsulation, the class has private fields but provides get and set methods for each field, so they can be accessed and modified from outside:
public final class User {
private String firstName;
private String lastName;
private int birthYear;

public User(String firstName, String lastName, int birthYear) {
this.firstName = firstName;
this.lastName = lastName;
this.birthYear = birthYear;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getBirthYear() {
return birthYear;
}

public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
}
Now, the same class written in Kotlin would look like this:
class User(var firstName: String,
var lastName: String,
var birthYear: String)
It may be hard to believe that the classes are considered equal; after all, the Java class has almost 30 lines of code and the Kotlin class would be only one line if we didn't use line breaks. But, if we take a look at the Java bytecode that the Kotlin compiler will produce for this class, we'll see that in fact they are equal. There is only one difference: the Kotlin class will have a couple of lines of Java bytecode more because we used non-nullable types in Kotlin, and the Kotlin compiler emits assertions that check arguments passed to functions are not null.
Now that we have seen how Kotlin is more concise than Java, let's explore the details.

Properties

A Kotlin class doesn't have setter or getter methods, yet it doesn't break encapsulation by exposing private fields. The reason is that Kotlin has properties. C# is one of the languages Kotlin drew inspiration from, and the concept of properties is one of the things it took from C#. You can think of properties as fields and methods, all in one place. To the outside users of a property, the syntax looks like they are accessing a public field of a class. Internally in your class, behind a property, you can have get and set accessors or methods and a private backing field. The get and set methods then control how your private field is accessed, thus not breaking encapsulation.
Specifying get and set, as in our case, is optional. If you omit them, then it is implied that the property is both read and write. The compiler generates a private backing field for you in that case. You can also have get only accessor inside a property. This is th...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Introducing Kotlin
  8. Kotlin Basics
  9. Classes and Object-Oriented Programming
  10. Functions and Lambdas
  11. Advanced Kotlin
  12. Kotlin Standard Library
  13. Coding a Dictionary App with Kotlin
  14. Other Books You May Enjoy