Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Object-Oriented Programming with Kotlin

You're reading from   Hands-On Object-Oriented Programming with Kotlin Build robust software with reusable code using OOP principles and design patterns in Kotlin

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781789617726
Length 370 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Abid Khan Abid Khan
Author Profile Icon Abid Khan
Abid Khan
Igor Kucherenko Igor Kucherenko
Author Profile Icon Igor Kucherenko
Igor Kucherenko
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with Kotlin 2. Introduction to Object-Oriented Programming FREE CHAPTER 3. The Four Pillars of Object-Oriented Programming 4. Classes - Advanced Concepts 5. Data Collection, Iterators, and Filters 6. Object-Oriented Patterns in Kotlin 7. Coroutines - a Lightweight Thread? 8. Interoperability 9. Regular Expression and Serialization in Kotlin 10. Exception Handling 11. Testing in Object-Oriented Programming with Kotlin 12. Assessments 13. Other Books You May Enjoy

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")
}
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime