Intro to Kotlin
Kotlin is a general purpose, open source, statically typed “pragmatic” programming language that combines object-oriented and functional programming features. You can use Kotlin to build applications for the Java Virtual Machine (JVM), Android, browsers, and native apps on MacOS, Linux, Windows, iOS, WebAssembly, and Android. Kotlin was created by JetBrains and released to open source under the Apache 2 license. Why learn Kotlin? The short answer is that Kotlin is a better language for the JVM than Java. Kotlin is less verbose, supports all the features of functional programming, eliminates the danger of null pointer references, streamlines the handling of null values, and maintains 100 percent interoperability with Java and Android. All of that will make you a more productive programmer than you would be writing Java, even if you start by using Kotlin to build new features into an existing Java application.
You can try Kotlin online without installing anything on your computer. On the try.kotlinlang.org site you might start by reading through and trying the examples, working through the Kotlin Koans, and then, if you want additional reinforcement, trying all the Kotlin in Action examples. Both the Problems section and the Koans provide unit tests and ask you to code answers that satisfy all of the tests. The knowledge needed to complete the Problems section is covered in previous sections; the Koans contain links to the appropriate Kotlin documentation in addition to a problem statement and a code skeleton. For extra credit, join Advent of Code and use the appropriate section of the Try Kotlin site to code your answers. You can log into both the Kotlin and Advent of Code sites with your existing social media credentials.
In addition to the online option, which is more useful for learning than for serious coding, the official Kotlin language site offers four local options for developing with Kotlin, as shown below. IntelliJ IDEA Community Edition (free) and Ultimate (subscription) both ship with the Kotlin plug-in. Android Studio, a free subset of IntelliJ IDEA that is specialized to Android development, has shipped with the Kotlin plug-in installed since version 3.0. You can install the plug-in into an older version if necessary. You can add a Kotlin plug-in to Eclipse (free), if that’s your standard Java IDE. I don’t recommend Eclipse for novices, as it can be hard to learn. At the same time, I don’t recommend that an experienced and happy Eclipse user switch to a different IDE. The command-line compiler, kotlinc, can be installed a number of different ways, depending on your system. You don’t need to install it if you are using IntelliJ IDEA, Android Studio, or Eclipse with the Kotlin plug-in. You do need to install it if you want to use a different editor or integrate with command-line Java build tools such as Gradle, Maven, or Ant. Once you have the Kotlin compiler installed, you can use it to build Java-compatible JAR files, either for use as library files or as runnable applications. You can also use the Kotlin compiler as a REPL interactive shell. There are also useful Kotlin plug-ins for other popular editors including Visual Studio Code and Sublime Text. These plug-ins supply syntax highlighting at a minimum. There’s also a Kotlin code completion (IntelliSense) plug-in for Visual Studio Code.
You can find a getting started tutorial on the official Kotlin site for IntelliJ IDEA, Eclipse, and the command-line compiler. In the screenshots below, I offer a slightly more complete tutorial for Kotlin using IntelliJ IDEA. I used the Ultimate edition; if you use the free Community edition, there are a couple of places where you will have to use a command line or another program to accomplish the same goals.
Also, all of the IntelliJ-based IDEs allow you to convert existing Java to Kotlin easily. In the online environment, you copy the Java, bring up a Convert from Java window, paste your code, and press the button to convert to Kotlin. Then select and copy the generated Kotlin code, close the window, and paste the Kotlin code to the destination. IntelliJ IDEA and Android studio eliminate the intermediate step of using the pop-up window. Just copy the Java and paste it into a Kotlin file, and the IDE will convert it automatically.
Kotlin Koan
The Kotlin Koans are a series of coding exercises for learning Kotlin syntax. There are three ways to play with them: online, in your IDE using the EduTools plug-in, and in your IDE or editor after cloning their GitHub repository. It’s worth completing all 42 Koans. It’s also worth installing EduTools so that you can go through other courses, either from Stepik or from other sources. The Koans, and all EduTools courses, combine a problem definition and code skeleton with unit tests and ask you to code answers that satisfy all the tests. You can author your own courses with EduTools at a later time if you wish. Among other things, it’s good practice for writing unit tests.
Kotlin for the server JVM
One of the most prevalent uses of Java is for application and web server. Kotlin fits beautifully into those scenarios, not least because it allows for incremental migration with full interoperability on the JVM. You really don’t want to mess with a million lines of working Java code without a good reason, so it’s important that you can add new Kotlin modules to a Java application without needing to change any existing code. In addition to interoperability and incremental migration, Kotlin offers improved scalability over Java code by using coroutines for asynchronous operations. At its core, suspending a coroutine has much less overhead than blocking a thread, since the mechanism to suspend and continue a coroutine is implemented at the compiler level as a state machine, with no context switch or any other need for intervention by the operating system. Several prominent frameworks now use Kotlin. One is Spring, which as of version 5 uses Kotlin to provide null safety for the entire framework and offers extensions to dramatically simplify some of the APIs when called from Kotlin. There are a couple of 100 percent Kotlin server apps currently in production. One of them is JetBrains’ own account app, which handles the license sales and validation process, and has run since 2015. Another is Corda, an open-source distributed ledger supported by major banks.
Kotlin for JavaScript
The second possible compilation target (or transpilation target, to be accurate) for Kotlin is JavaScript. You can use the generated JavaScript in a webpage to manipulate the DOM and interact with web graphics, for example WebGL. You can also use the transpiled JavaScript in a Node.js app on a server or desktop. There is special support for Kotlin and React including the Kotlin React bindings and the Create React Kotlin App. For strong typing, you can convert TypeScript definitions from the Definitely Typed type definitions repository to Kotlin using the ts2kt tool. For weak typing, use the dynamic type.
Kotlin for Android
Google and JetBrains officially support Kotlin in Android Studio and IntelliJ IDEA, and several major Android apps are now written either entirely (Basecamp) or partially (Pinterest) in Kotlin. Pinterest reports that it completely eliminated null pointer exceptions (NPEs) after adopting Kotlin. Another major app, Keepsafe, was originally written in Java, then converted entirely to Kotlin, leading to a 30 percent decrease in source line count and a 10 percent decrease in method count. In order to develop Android Dalvik Virtual Machine (DVM) apps in Kotlin you basically need to install one of the two IDEs I mentioned, plus the Android SDK. To try out your apps, you’ll need an Android device or some Android emulators.
Kotlin for compiled native apps
Finally, you can use Kotlin in combination with the LLVM compiler to create native applications without a dependency on a virtual machine for Windows, Linux, MacOS, iOS, WebAssembly, and Android. Yes, that’s right, you can build compiled Kotlin native apps for Android in addition to building Kotlin DVM apps. Whether you’ll want to do that is another question, since Android has the DVM built-in. According to JetBrains, “Kotlin/Native is primarily designed to allow compilation for platforms where virtual machines are not desirable or possible (such as iOS or embedded targets), or where a developer is willing to produce a reasonably-sized self-contained program without the need to ship an additional execution runtime.” Kotlin can provide huge benefits to developers currently working on Java and Android projects, starting with the blanket elimination of null pointer exceptions, continuing to full support for functional programming, and on to allowing you to write less code than you would in Java. Kotlin also works well when compiled to native code or transpiled to JavaScript.
The bottom line is there are no serious drawbacks to Kotlin compared to Java.
Architect & Vice President, Java Platform Group at Oracle
6 年eschewed in Java due to the (questionable) experiences with OO in C++