Programming Kotlin Applications. Бретт Мак-Лахлин

Читать онлайн.
Название Programming Kotlin Applications
Автор произведения Бретт Мак-Лахлин
Жанр Программы
Серия
Издательство Программы
Год выпуска 0
isbn 9781119696216



Скачать книгу

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.

      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.

      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.

      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() }

      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.

      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

Snapshot of 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.

Snapshot of 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.

      WARNING