There are several editors that support Kotlin, but IntelliJ IDEA gives the best of breed experience. Learn how to begin a mixed Java and Kotlin project using the community edition of IntelliJ IDEA.
- [Instructor] Using IntelliJ IDEA makes it easy to start your Kotlin projects. I highly recommend using Gradle to build your projects. Here's how to get started. From the Welcome to IntelliJ IDE screen, choose Create New Project, or from the IDE choose File, New, Project. On the New Project screen, choose Gradle in the left column. Resist the temptation to select Kotlin or Java. Under Additional Libraries and Frameworks, check both Java and Kotlin Java.
On the new project page, give your project a group ID and an artifact ID. Considering that my app's name is Demo, I'm going to call mine com.tekadept.demo and my artifact ID will will be demo. The artifact ID is a human-readable name of your project or app. Under the New Project, be sure to check create directories for empty content roots automatically, and make sure that create separate modules per source set, and use default Gradle wrapper recommended are all checked.
Then click next, and then click Finish. Once IntelliJ and Gradle are finished, we will have a project which uses the standard Java Maven build structure. We should keep all of our Kotlin files in the Kotlin directory, and do likewise with our Java files. Keep in mind that while it is recommended to stick to the Java project structure, Kotlin doesn't require it. Unlike Java, Kotlin packages don't have to match their directory structure.
Classes don't have to match their file names, and you are not restricted to one class per file, but interoperability is easier when we do, so we'll stick to a Java project structure. Now let's add some code. Right click on source, main, Kotlin, and select New, Kotlin File or Class. Enter the following.
Once again, we want to give our package name and the name of our class. So, in this case, our package name is com.tekadept.demo and our class name is main, and when we click OK. Now let's add some code. We'll type in F-U-N, fun, for function. The name of the function, in this case, this is going to be our main function, where our Kotlin program will start execution from. We're going to type in args for arguments, and array string, since the arguments that are going to come in are going to be an array of string.
Open curly brace, closed curly brace, and then inside of it, we're going to type, and this is, just again, our very familiar hello, world. Make sure everything is building correctly. Hello, world. To execute this code, we do a Control + Shift + R. That'll cause it to build and execute. And, down below, we can see our output of Hello, World. Please note that I have presentation assist enabled.
It shows which command keys I am typing in the lower center of the screen. If you are interested, it is available as an IntelliJ plugin.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin