Kotlin In-Depth
eBook - ePub

Kotlin In-Depth

A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile

Aleksei Sedunov

Compartir libro
  1. English
  2. ePUB (apto para móviles)
  3. Disponible en iOS y Android
eBook - ePub

Kotlin In-Depth

A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile

Aleksei Sedunov

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

It's easy to learn, so you can start making powerful apps right away.

Key Features
? Numerous code samples covering all aspects of the Kotlin language.
? Coverage on native applications, web apps, microservices, and app testing.
? Step-by-step instructions are provided in a clear and concise manner.

Description
The book 'Kotlin In-Depth, Second Edition' updates all the essential parts of Kotlin and incorporates modern principles, methodologies, and approaches for achieving efficient solutions. The book will guide you to successfully utilize Kotlin in developing JVM apps for desktop, mobile, web platforms and transferring existing Java codebases to Kotlin.The book begins with an introduction to the language and its environment, which will help you to grasp the fundamental concepts underlying Kotlin's design. The readers will learn the Kotlin tooling and the language's core syntax and structures. The book teaches Kotlin's multi-paradigm nature, which enables the creation of powerful abstractions by mixing parts of functional and object-oriented programming. This book discusses how to use standard Kotlin APIs like the standard library, reflection, and coroutine-based concurrency, as well as how to create your flexible APIs using domain-specific languages. The book demonstrates how to use Kotlin for more specific tasks such as testing, developing Android applications, developing Web applications, and developing microservices.After reading this book, you'll be prepared to dive deeper into the Kotlin ecosystem's more specialized areas, including Android applications, server-side development, native programming, and code sharing across different platforms.

What you will learn
? Acquire a deep understanding of all fundamental features of Kotlin programming.
? Utilize object-oriented and functional capabilities to create a flexible and reusable codebase.
? Leverage the Kotlin standard library to create custom domain-specific languages.
? Implement the Kotlin coroutines package to write asynchronous programming.
? A solid foundation of relevant development platforms, tools, and frameworks.

Who this book is for
The book is primarily geared towards Java and JVM developers who want to learn Kotlin and explore modern and efficient development techniques. Knowing the basics of programming is helpful but not necessary.

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. Exploring Collections and I/O
8. Understanding Class Hierarchies
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 Microservices

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Kotlin In-Depth un PDF/ePUB en línea?
Sí, puedes acceder a Kotlin In-Depth de Aleksei Sedunov en formato PDF o ePUB, así como a otros libros populares de Informatik y Programmierung in Java. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2022
ISBN
9789391030636

CHAPTER 1

Kotlin - Powerful and Pragmatic

This chapter is meant to explain the major features which make Kotlin an excellent and efficient language for modern application development and the reasons why you might want to learn it. We’ll learn the basic ideas which stand behind the Kotlin design and 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

We will cover the following topics:
  • What is Kotlin?
  • Major components of the Kotlin ecosystem
  • Setting up a Kotlin project in IDE and online editors

Objectives

At the end of the chapter, you’ll get an understanding of the basic Kotlin principles and the Kotlin ecosystem as well as what simple a Kotlin program looks like and you will be able 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 had reached its first release in February 2016 and has been steadily becoming an increasingly popular and promising tool in many development areas such as Android development, desktop applications, or server-side solutions. The company which stands behind the language and has been investing in its development ever since is JetBrains which is famous for its excellent software engineering tools such as IntelliJ IDEA. By August 2020, Kotlin had reached version 1.4, acquiring massive community, well-developed ecosystems, 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 announced Kotlin an officially supported language of the Android platform which gave a tremendous boost to the language popularity. Nowadays, a lot of companies – among them Google, Amazon, Netflix, Pinterest, Uber, and many others – are using Koltin for production development, and the number of open positions for Kotlin developers is growing steadfast.
It all became possible thanks to the efforts devoted to the 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 time, JetBrains had already accumulated an extensive Java code base for products centered around its IntelliJ platform which was 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 evolution and lack of many useful features which at that moment had already been 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 implementation of their own language. The new language was eventually named Kotlin as a tribute to an island near Saint-Peteresburg, Russia where most of its development team was 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 a multiparadigm language emphasizing safety, conciseness, and interoperability. Let’s look at these traits in more detail.

Safe

For a programming language, being safe means being able to prevent a 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 allow it to spend more time reasoning about it correctness (probably both). One of Kotlin design goals was to find a sort of golden mean; contriving a language with more stronger safety guarantees than Java, but not so strong to frustrate a developer’s productivity. And although the Kotlin solution is by no means absolute, it has repeatedly proved to be an efficient choice in practice.
We’ll discuss 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 multiparadigmality implied the support of functional programming in addition to the conventional object-oriented paradigm typical for 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 which means 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 an 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 an extensive API facilitating the functional style. Nowadays, many of that also apply to Java which had introduced functional programming support starting with Java 8. But its expressiveness still somewhat falls behind Kotlin’s.
We’ll cover the basics of functional programming in Chapter 5, Leveraging Advanced Functions and Functional Programming, but its applications and examples will accompany us throughout the book.
Over its growth, the language also began to exhibit two more programming paradigms. Thanks to the ability to design APIs in the 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, the 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 this approach in Chapter 13, Concurrency.
All in all, the presence of multiple paradigms greatly increases the language’s 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 the existing code generally takes more time than writing a new one and that’s why language conciseness and the ability to clearly express a 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, but 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 simplify the description of specific programming domains with a minimum of syntactic noise.

Interoperable

Java interoperability was a major point in Kotlin design since the Kotlin code wasn’t mean to exist in isolation, but to cooperate as smoothly as possible...

Índice