An Introduction to Kotlin

Android development is performed primarily using Android Studio which is, in turn, based on the IntelliJ IDEA development environment created by a company named JetBrains. Before the release of Android Studio 3.0, all Android apps were written using Android Studio and the Java programming language (with some occasional C++ code when needed).

Since the introduction of Android Studio 3.0, however, developers now have the option of creating Android apps using another programming language called Kotlin. Although detailed coverage of all features of this language is beyond the scope of this book (entire books can and have been written covering solely Kotlin), the objective of this and the following six chapters is to provide enough information to begin programming in Kotlin and quickly get up to speed developing Android apps using this programming language.

What is Kotlin?

Named after an island located in the Baltic Sea, Kotlin is a programming language created by JetBrains and follows Java in the tradition of naming programming languages after islands. Kotlin code is intended to be easier to understand and write and also safer than many other programming languages. The language, compiler, and related tools are all open source and available for free under the Apache 2 license.

The primary goals of the Kotlin language are to make code both concise and safe. Code is generally considered concise when it can be easily read and understood. Conciseness also plays a role when writing code, allowing code to be written more quickly and with greater efficiency. In terms of safety, Kotlin includes several features that improve the chances that potential problems will be identified when the code is being written instead of causing runtime crashes. A third objective in the design and implementation of Kotlin involves interoperability with Java.

Kotlin and Java

Originally introduced by Sun Microsystems in 1995, Java is still by far the most popular programming language in use today. Until the introduction of Kotlin, it is quite likely that every Android app available on the market was written in Java. Since acquiring the Android operating system, Google has invested heavily in tuning and optimizing compilation and runtime environments for running Java-based code on Android devices.

 

 

You are reading a sample chapter from an old edition of the Jetpack Compose Essentials book.

Purchase the fully updated Jetpack Compose 1.7 Essentials edition of this book in eBook or Print format.

The full book contains 67 chapters and over 700 pages of in-depth information.

Buy the full book now in Print or eBook format. Learn more.

Preview  Buy eBook 

 

Rather than try to reinvent the wheel, Kotlin is designed to both integrate with and work alongside Java. When Kotlin code is compiled, it generates the same bytecode as that generated by the Java compiler enabling projects to be built using a combination of Java and Kotlin code. This compatibility also allows existing Java frameworks and libraries to be used seamlessly from within Kotlin code and also for Kotlin code to be called from within Java.

Kotlin’s creators also acknowledged that while there were ways to improve on existing languages, there are many features of Java that did not need to be changed. Consequently, those familiar with programming in Java will find many of these skills to be transferable to Kotlin-based development. Programmers with Swift programming experience will also find much that is familiar when learning Kotlin.

Converting from Java to Kotlin

Given the high level of interoperability between Kotlin and Java, it is not essential to convert existing Java code to Kotlin since these two languages will comfortably co-exist within the same project. That being said, Java code can be converted to Kotlin from within Android Studio using a built-in Java to Kotlin converter. To convert an entire Java source file to Kotlin, load the file into the Android Studio code editor and select the Code -> Convert Java File to Kotlin File menu option. Alternatively, blocks of Java code may be converted to Kotlin by cutting the code and pasting it into an existing Kotlin file within the Android Studio code editor. Note when performing Java to Kotlin conversions that the Java code will not always convert to the best possible Kotlin code and that time should be taken to review and tidy up the code after conversion.

Kotlin and Android Studio

Support for Kotlin is provided within Android Studio via the Kotlin Plug-in which is integrated by default into Android Studio 3.0 or later.

Experimenting with Kotlin

When learning a new programming language, it is often useful to be able to enter and execute snippets of code. One of the best ways to do this with Kotlin is to use the Kotlin Playground (Figure 11-1) located at https://play. kotlinlang.org:

 

 

You are reading a sample chapter from an old edition of the Jetpack Compose Essentials book.

Purchase the fully updated Jetpack Compose 1.7 Essentials edition of this book in eBook or Print format.

The full book contains 67 chapters and over 700 pages of in-depth information.

Buy the full book now in Print or eBook format. Learn more.

Preview  Buy eBook 

 

Figure 11-1

In addition to providing an environment in which Kotlin code may be quickly entered and executed, the playground also includes a set of examples and tutorials demonstrating key Kotlin features in action.

Try out some Kotlin code by opening a browser window, navigating to the playground, and entering the following into the main code panel:

fun main(args: Array<String>) {
    
   println("Welcome to Kotlin")
    
   for (i in 1..8) {    
       println("i = $i")
   } 
}Code language: Kotlin (kotlin)

After entering the code, click on the Run button and note the output in the console panel: 96

An Introduction to Kotlin

Figure 11-2

Semi-colons in Kotlin

Unlike programming languages such as Java and C++, Kotlin does not require semi-colons at the end of each statement or expression line. The following, therefore, is valid Kotlin code:

 

 

You are reading a sample chapter from an old edition of the Jetpack Compose Essentials book.

Purchase the fully updated Jetpack Compose 1.7 Essentials edition of this book in eBook or Print format.

The full book contains 67 chapters and over 700 pages of in-depth information.

Buy the full book now in Print or eBook format. Learn more.

Preview  Buy eBook 

 

val mynumber = 10 
println(mynumber)Code language: Kotlin (kotlin)

Semi-colons are only required when multiple statements appear on the same line:

val mynumber = 10; println(mynumber)Code language: Kotlin (kotlin)

Summary

For the first time since the Android operating system was introduced, developers now have an alternative to writing apps in Java code. Kotlin is a programming language developed by JetBrains, the company that created the development environment on which Android Studio is based. Kotlin is intended to make code safer and easier to understand and write. Kotlin is also highly compatible with Java, allowing Java and Kotlin code to co-exist within the same projects. This interoperability ensures that most of the standard Java and Java-based Android libraries and frameworks are available for use when developing using Kotlin.

Kotlin support for Android Studio is provided via a plug-in bundled with Android Studio 3.0 or later. This plugin also provides a converter to translate Java code to Kotlin.

When learning Kotlin, the online playground provides a useful environment for quickly trying out Kotlin code.


Categories