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

Чтение книги онлайн.

Читать онлайн книгу Programming Kotlin Applications - Бретт Мак-Лахлин страница 10

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

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

lastName=Truesby) is attending! User(firstName=Rose, lastName=Bushnell) is attending!

      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.

      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.

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