Book Image

Hands-On Object-Oriented Programming with Kotlin

By : Abid Khan, Igor Kucherenko
Book Image

Hands-On Object-Oriented Programming with Kotlin

By: Abid Khan, Igor Kucherenko

Overview of this book

Kotlin is an object-oriented programming language. The book is based on the latest version of Kotlin. The book provides you with a thorough understanding of programming concepts, object-oriented programming techniques, and design patterns. It includes numerous examples, explanation of concepts and keynotes. Where possible, examples and programming exercises are included. The main purpose of the book is to provide a comprehensive coverage of Kotlin features such as classes, data classes, and inheritance. It also provides a good understanding of design pattern and how Kotlin syntax works with object-oriented techniques. You will also gain familiarity with syntax in this book by writing labeled for loop and when as an expression. An introduction to the advanced concepts such as sealed classes and package level functions and coroutines is provided and we will also learn how these concepts can make the software development easy. Supported libraries for serialization, regular expression and testing are also covered in this book. By the end of the book, you would have learnt building robust and maintainable software with object oriented design patterns in Kotlin.
Table of Contents (14 chapters)

Introduction to Kotlin

In 2010, Jet Brains started working on a project called Project Kotlin. This aimed to develop a language that is easy, concise, and expressive. This is also a language that can help to improve productivity without compromising on quality, including that of backward compatibility and interoperability with existing code bases. Other than freedom from semicolons, there are a number of reasons to use Kotlin.

For a start, a beginner programmer can feel as comfortable as an experienced developer when using it. It also compiles to Java-6 bytecode, which allows a developer to use advanced features such as lambda expressions with legacy code. Kotlin is 100% interoperable with Java, so it is possible to call Kotlin code in Java as well as calling Java code in Kotlin. Furthermore, it is concise and expressive, and it helps avoid the boilerplate code that is required in Java. Kotlin is safe, therefore most Null Pointer Exceptions (NPEs) can be avoided. By default, it is not allowed to assign null values to variables. If the type of the variable is verified at compile time, the language is considered to be statically typed. Kotlin is a statically typed language and the benefit of this is that all tricky and trivial bugs can be caught at an early stage. With dynamically typed languages, type checking is performed at runtime. Perl, Smalltalk, and Ruby belong to a dynamically typed language group. Kotlin has great tooling support because it is a product of Jet Brains, a company renowned for providing IDEs for development. In addition, Kotlin supports Android because it is officially supported by Google. Kotlin supports Kotlin/Native technology for compiling Kotlin code in native binaries which does not rely on virtual machine and Kotlin supports browsers because all modern languages should work with JavaScript. A number of big brands (including Pinterest, Uber, Gradle, and Evernote) have started using Kotlin as a main language, and they feel that it helps to improve their productivity and quality of code.

With the help of Kotlin, we will now create our first hello world application as follows:

  1. Start IntelliJ IDE and click on File.
  2. Click on the New option and then click on the Project in the Menu.
  3. From the left pane in the newly opened window, select Kotlin | Kotlin/JVM and then press Next.
  4. Assign a project name and location, making sure that the latest SDK is selected.
  1. Click on Finish. IntelliJ IDE will open a new window with preconfigured files and folders. src is a source folder where all Kotlin files will be added.
  2. Right-click on src and select New. Under this, click on Kotlin File/Class.
  3. Assign a name, select a file from the Kind menu, and press OK.
  4. Add the following code in the newly opened window:
fun main(args: Array<String>) {
println("Hello world")
}