Название: Programming Kotlin Applications
Автор: Бретт Мак-Лахлин
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119696216
isbn:
Obviously, parts of this probably look odd to you, whether you're brand new to writing code or an experienced Java pro. On top of that, it's likely you have no idea how to actually compile or run this code. That's OK, too. We'll get to all of that.
NOTE It bears repeating: You really don't need to understand the code in Listing 1.1. This book assumes you've programmed at least a little bit—and it's true that you'll likely understand Kotlin a bit faster if you have a Java background—but you will pick up everything you see in Listing 1.1 (and quite a bit more) just by continuing to read and working through the code samples. Just keep going, and you'll be programming in Kotlin in no time.
For now, though, here's the point: Kotlin is really approachable, clean to read, and actually a pretty fun language to use. 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.