Programming Kotlin Applications. Бретт Мак-Лахлин
With that in mind, let's get some basics out of the way so you can get to writing code, not just looking at it.
WHAT IS KOTLIN?
Kotlin is an open-source programming language. It is, most notably, statically typed and object-oriented. Statically typed means that variables have types when you write your code and compile it, and those types are fixed. That also implies that Kotlin must be compiled, which is also true. Object-oriented means it has classes and inheritance, making it a familiar language for Java and C++ developers.
Kotlin was in fact created by a group of developers that worked on the JetBrains IDE, and it feels very much like a natural evolution of Java. It's been around in early form since 2011, but was officially released in 2016. That means it's new, which is good, but also means it's new, which at times can be bad. Kotlin is modern, can run inside a Java Virtual Machine (JVM), and can even be compiled to JavaScript—a cool feature we'll look at a little later.
It's also really important to note that Kotlin is a fantastic language for writing Android apps. In fact, many of its enhancements to Java reflect an Android usage. That said, even if you never intended to write a mobile app, you'll find Kotlin a welcome addition to your arsenal, and well suited for server-side programming.
What Does Kotlin Add to Java?
That's a good question that has a long answer. In fact, we'll spend most of this book answering that in various forms. But, for most, Kotlin adds or changes a few key features when compared to Java:
NOTE If you're new to Kotlin or not coming from a Java background, feel free to skip right on to the next section.
Kotlin ditches NullPointerException (and nullable variables altogether) in almost all situations.
Kotlin supports extending functions without having to entirely override a parent class.
Kotlin doesn't support checked exceptions (you may not find this to be an advancement, so fair warning).
Kotlin adds components of functional programming, such as extensive lambda support and lazy evaluation.
Kotlin defines data classes that let you skip writing basic getters and setters.
There's certainly a lot more to this list, but you can quickly see that Kotlin isn't just a slightly different version of Java. It seeks to be different and better, and in many ways, it very much is exactly that.
KOTLIN IS OBJECT-ORIENTED
At this point, most books and tutorials would have you put together a simple “Hello, World” program. That's all well and good, but the assumption here is that you want to get moving, and get moving quickly. For that reason, the logical place to begin with Kotlin is by creating an object.
An object is simple a programmatic representation of a thing. In the best case, that thing is a real-world object, like a car or a person or a product. For example, you could create an object to model a person like this:
class Person { /* This class literally does nothing! */ }
That's it. You can now create a new variable of type Person
like this:
fun main() { val jennifer = Person() }
If you put all this together into a single code listing, you'll have Listing 1.2.
LISTING 1.2: A very useless object in Kotlin (and a main function to use it)
class Person { /* This class literally does nothing! */ } fun main() { val jennifer = Person() }
Now, this is pretty lame code, honestly. It doesn't do anything, but it is object-oriented. Before we can improve it, though, you need to be able to run this good-for-almost-nothing code yourself.
INTERLUDE: SET UP YOUR KOTLIN ENVIRONMENT
Getting a Kotlin program to run is relatively easy, and if you're an old Java hand, it's actually really easy. You'll need to install a Java Virtual Machine and then a Java Development Kit (JDK). Then you'll want one of the numerous IDEs that support Kotlin. Let's take a blazing-fast gallop through that process.
Install Kotlin (and an IDE)
One of the easiest IDEs to use with Kotlin is IntelliJ IDEA, and starting with version 15, IntelliJ comes bundled with Kotlin. Plus, since IntelliJ is actually from JetBrains, you're getting an IDE built by the same folks who came up with Kotlin itself.
Install IntelliJ
You can download IntelliJ from www.jetbrains.com/idea/download
. This page (shown in Figure 1.1) will then redirect you to the appropriate platform (Mac OS X for most of this book's examples). Download the free Community version to get started without any cost. Once the (rather large) download completes, install it (see Figure 1.2), and you'll get a Java Runtime Environment (JRE) and the JDK as part of installation.
FIGURE 1.1 Download IntelliJ from the JetBrains download page.
NOTE IntelliJ is not the only IDE that works with Kotlin, and the list is actually growing pretty quickly. Other notable options are Android Studio ( developer.android.com/studio/preview/index.html
) and Eclipse ( www.eclipse.org/downloads
). Eclipse in particular is immensely popular, but IntelliJ is still a great choice as it shares the JetBeans heritage with Kotlin.
FIGURE 1.2 IntelliJ comes prepackaged with a system-specific installation process.
NOTE The “installation process” for IntelliJ on Mac OS X is pretty simple: just drag the package (presented as an icon) into your Applications folder. You'll then need to go to that folder and launch IntelliJ or drag the icon into your Dock, which is what I've done.
For Windows, you download the executable and run it. You can then create a shortcut on your desktop if you like.
In both cases, you can use the JetBrains Toolbox (which comes with the JetBrains Kotlin package) to keep your installation current and add updates when they're available.
You'll be given a pretty large number of options to get your IDE set up. For IntelliJ, you'll pick a UI theme (either is fine), a Launcher Script (I'd suggest you accept the default and let it create the script), the default plugins, and a set of featured plugins. You can click through these quickly, and then your IDE will restart. You'll see a welcome screen similar to Figure 1.3, and you should select Create New Project.
WARNING