Kotlin In-Depth [Vol-I]
eBook - ePub

Kotlin In-Depth [Vol-I]

A Comprehensive Guide to Modern Multi-Paradigm Language

Aleksei Sedunov

Share book
  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Kotlin In-Depth [Vol-I]

A Comprehensive Guide to Modern Multi-Paradigm Language

Aleksei Sedunov

Book details
Book preview
Table of contents
Citations

About This Book

Master the concise and expressive power of a pragmatic, multi-paradigm language for JVM, Android and beyond Key Features

  • Language fundamentals
  • Object-oriented and functional programming with Kotlin
  • Kotlin standard library
  • Building domain-specific languages
  • Using Kotlin for Web development
  • Kotlin for Android platform
  • Coroutine-based concurrency

  • Description
    The purpose of this book is to guide a reader through the capabilities of Kotlin language and give examples of how to use it for the development of various applications, be it desktop, mobile or Web. Although our primary focus is on JVM and Android, the knowledge we're sharing here, to various extents, applies to other Kotlin-supported platforms such as JavaScript, native and even multi-platform applications.The book starts with an introduction to the language and its ecosystem, which will give you an understanding of the key ideas behind the Kotlin design, introduce you to the Kotlin tooling and present you the basic language syntax and constructs. In the next chapters, we get to know the multi-paradigm nature of Kotlin which allows us to create powerful abstractions by combining various aspects of functional and object-oriented programming. We'll talk about using common Kotlin APIs, such as the standard library, reflection, and coroutine-based concurrency as well as the means for creating your own flexible APIs based on domain-specific languages. What you will learn
    By the end of the book you'll obtain a thorough knowledge of all the basic aspects of Kotlin programming. You'll be able to create a flexible and reusable code by taking advantage of object-oriented and functional features, use Kotlin standard library, compose your own domain-specific languages, write asynchronous code using Kotlin coroutines library as well. You'll also have a basic understanding of using Kotlin for writing test code, web applications and Android development. This knowledge will also give you a solid foundation for deeper learning of related development platforms, tools, and frameworks. Who this book is for
    The book is primarily aimed at developers who are familiar with Java and JVM and are willing to get a firm understanding of Kotlin while having little to no experience in that language. Discussion of various language features will be accompanied, if deemed necessary, by comparisons with their Java's analogs, which should simplify the Java-to-Kotlin transition. Most of the material, however, is rather Java-agnostic and should be beneficial even without prior knowledge of Java. In general, experience in object-oriented or functional paradigm is a plus, but not required. Table of Contents
    1. Kotlin: Powerful and Pragmatic
    2. Language Fundamentals
    3. Defining Functions
    4. Working with Classes and Objects
    5. Leveraging Advanced Functions and Functional Programming
    6. Using Special-Case Classes
    7. Understanding Class Hierarchies
    8. Exploring Collections and I/O
    9. Generics
    10. Annotations and Reflection
    11. Domain-Specific Languages
    12. Java Interoperability
    13. Concurrency
    14. Testing with Kotlin
    15. Android Applications
    16. Web Development with Ktor
    17. Building Microservice About the Author
    Aleksei Sedunov has been working as a Java developer since 2008. Since joining JetBrains in 2012, he's been actively participating in the Kotlin language development, focusing on IDE tooling for the IntelliJ platform. Currently, he's working in a DataGrip team, a JetBrains Database IDE, while carrying on with Kotlin as a main development tool. LinkedIn Profile: https://www.linkedin.com/in/alexey-sedunov-8554a530/

Frequently asked questions

How do I cancel my subscription?
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.
Can/how do I download books?
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.
What is the difference between the pricing plans?
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.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is Kotlin In-Depth [Vol-I] an online PDF/ePUB?
Yes, you can access Kotlin In-Depth [Vol-I] by Aleksei Sedunov in PDF and/or ePUB format, as well as other popular books in Informatik & Programmierung in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9789389328585

CHAPTER 1

Kotlin: Powerful and Pragmatic

This chapter is meant to explain the major features that make Kotlin an excellent and efficient language for modern application development and why you would want to learn it. We’ll learn the basic ideas which stand behind the Kotlin design, get an overview of Kotlin libraries and frameworks for different application areas, such as Android applications, concurrency, testing and web development. In conclusion, we’ll guide you through the steps required to set up a Kotlin project in two popular development environments, IntelliJ IDEA and Eclipse, and introduce you to the interactive Kotlin shell.

Structure

  • What is Kotlin?
  • Major components of Kotlin ecosystem
  • Setting up a Kotlin project in IntelliJ and Eclipse

Objective

At the end of the chapter you’ll get an understanding of the basic principles and ecosystem of Kotlin as well as how a simple Kotlin program looks like and how to set up a project in common IDEs.

What is Kotlin?

Kotlin is a multiplatform and multiparadigm programming language emphasizing safety, conciseness and interoperability. Conceived in late 2010, it was first released in February, 2016, and has since steadily become increasingly popular and promising a tool in many development areas, be it an Android development, desktop applications, or server-side solutions. The company which stands behind the language and has been investing into its development ever since is JetBrains, famous for its excellent software engineering tools, such as IntelliJ IDEA.
By November, 2019, Kotlin had reached version 1.3, acquiring a massive community, well-developed ecosystem and extensive tooling. Having overgrown an original intent of creating a better Java alternative, it now embraces multiple platforms, including Java Virtual Machine, Android, JavaScript and native applications. In 2017, Google had announced Kotlin as an officially-supported language of Android platform, which gave a tremendous boost to the language’s popularity. Nowadays, a lot of companies – including Google, Amazon, Netflix, Pinterest, Uber and many others – are using Kotlin for production development, and the number of open positions for Kotlin developers is growing steadfastly.
It all became possible due to efforts devoted to careful language design and putting into action the primary traits, which make Kotlin such an excellent development tool. The language philosophy has mainly arisen based on the problems it was intended to solve back in 2010. By that moment, JetBrains had already accumulated an extensive Java code base for products centered around its IntelliJ platform, which apart of arguably the most known IntelliJ IDEA, had also included a set of minor IDEs dedicated to different technologies such as WebStorm, PhpStorm, RubyMine, etc.
The maintenance and growth of such codebase, however, was being hampered by Java itself due to its slow pace of evolution and lack of many useful features which at that moment had already been made available in such languages as Scala and C#. Having researched the JVM languages available at that moment, the company concluded that no existing language proved satisfiable for their needs and decided to invest resources into implementing their own language. The new language was eventually named Kotlin as a tribute to an island near Saint-Petersburg, Russia, where most of its development team is located.
So, what are those traits which have been shaping the language from the very beginning? In fact, we’ve already given the answer in its definition. The reason behind Kotlin is a need for multiparadigm language that emphasizes safety, conciseness and interoperability. Let’s look at these traits in more detail.

Safe

For a programming language to be safe requires it to prevent programmer’s errors. In practice, designing the language with respect to safety is a matter of tradeoff since error prevention typically comes at a cost: you give the compiler more detailed information about your program or let it spend more time reasoning about its correctness (probably both). One of the Kotlin design goals was to find a sort of a golden mean: contriving a language with stronger safety guarantees than Java, but not so strong to frustrate a developer’s productivity. And although Kotlin solution is by no means absolute, it has repeatedly proved to be an efficient choice in practice.
We’ll discuss the various aspects of Kotlin safety as we go through the book. Here we’d like to point out some major features:
  • Type inference which allows the developer to omit explicit declaration types in most cases (Java 10 introduced this for local variables);
  • Nullable types regulate the usage of null and help to prevent infamous NullPointerException;
  • Smart casts which simplify type casting, reducing the chance of casting errors at runtime

Multiparadigm

Initially, the meaning behind Kotlin multi paradigmality implied the support of functional programming in addition to conventional object-oriented paradigm that is typical of many mainstream programming languages such as Java. The functional programming is based around the idea of using functions as values: passing them as parameters or returning from other functions, declaring locally, storing in variables, etc. Another aspect of the functional paradigm is an idea of immutability, meaning that objects you manipulate can’t change their state once created and functions can’t produce side effects.
The major benefit of this approach is improved programming flexibility: being able to create a new kind of abstraction, you can write more expressive and concise code, thus increasing your productivity.
Note that although functional programming principles can be employed in many languages (Java’s anonymous classes, for example, were obvious choice before introduction of lambdas), not every language has necessary syntactic facilities encouraging the writing of such code.
Kotlin, on the contrary, included necessary features right from the start. They include, in particular, functional types smoothly integrating functions into the language type systems and lambda expressions meant to create functionally-typed values from code blocks. The standard library as well as external frameworks provides extensive API, facilitating the functional style. Nowadays, many of that also apply to Java which had introduced functional programming support starting with Java 8. It’s expressiveness, though, still somewhat falls behind that of Kotlin’s.
We’ll cover the basics of functional programming in Chapter 5, but its applications and examples will accompany us throughout all the remaining of the book.
Over its growth, the language also began to exhibit two more programming paradigms. Thanks to the ability to design APIs in a form of domain-specific languages (DSLs), Kotlin can be used in a declarative style. In fact, many Kotlin frameworks provide their own DSLs for specific tasks with no need to sacrifice type-safety or expressive power of the general-purpose programming language. For example, exposed framework includes a DSL for defining database schema and manipulating its data, whereas kotlinx.html gives a concise and type-safe alternative to HTML template languages. In Chapter 11, Domain-Specific Languages, we’ll discuss these examples in more detail as well as learn how to create our own DSLs.
One more paradigm, namely, concurrent programming, entered the language with the introduction of coroutines. Although concurrency support by itself is present in many languages, including Java, the Kotlin features a rich set of programming patterns which enable a new programming approach. We’ll cover the basics of concurrency in Chapter 13, Concurrency.
All in all, the presence of multiple paradigms greatly increase a language expressive power, making it a more flexible and multi-purposed tool.

Concise and expressive

Developer productivity is largely tied with the ability to quickly read and understand the code, be it some other developer’s work or maybe your own after a significant time has passed. In order to understand what a specific piece of code does, you need to also understand how it’s related to other parts of your program. That’s why reading existing code generally takes more time than writing a new one and that’s why language conciseness, an ability to clearly express programmer’s intents without much information noise is a crucial aspect of language efficiency as a development tool.
The designers of Kotlin did their best to make language as concise as possible, eliminating a lot of notorious Java boilerplate, such as field getters and setters, anonymous classes, explicit delegation and so on. On the other hand, they made sure the conciseness is not overtly abused – unlike Scala, for example, Kotlin doesn’t allow the programmer to define custom operators, only redefine existing ones since the former tends to obfuscate the operation meaning. In the course of the book we’ll see numerous implications of this decision and how useful it turned to be.
Another aspect of Kotlin’s conciseness is tightly related to the DSLs (see Chapter 11, Domain-Specific Languages), which greatly simplifies the description of specific programming domains with a minimum of syntactic noise.

Interoperable

Java interoperability was a major point in Kotlin design, since Kotlin code wasn’t mean to exist in isolation, but to cooperate as smoothly as possible with existing codebase. That’s why Kotlin designers made sure that not only existing Java code could be used from Kotlin, but that Kotlin code could also be used from Java with little to no effort. The language also includes a set of features specifically meant to tune interoperability between Java and Kotlin.
As the language outgrew the JVM and spread to other platforms, interoperability guarantees were extended to cover interaction with JavaScript code for JS platform and C/C++/Objective C/Swift code for native applications.
We’ll devote Chapter 12, Java Interoperability, to discuss Java interoperability issues and how Kotlin and Java can be mixed together in the same project.

Multiplatform

Multi platformity wasn’t an original intent of Kotlin designers, but rather manifested itself as a result of language evolution and adaptation to the needs of the development community. While JVM and Android remain primary targets of Kotlin deve...

Table of contents