Setting up the development environment

Although you could use a basic text editor for writing Kotlin source code and compile it using the command-line tools, you will make your life easier if you use an IDE for coding. IDE can provide code completion, syntax highlighting, stepping through code while debugging, refactoring, and more.

IntelliJ IDEA is probably the most popular IDE for Java today. The IDE comes from the same company that created Kotlin and, of course, Kotlin is a first class citizen inside IntelliJ. This book uses IntelliJ, and it is recommended that you use it as well. But other popular IDEs for Java could also work, such as Eclipse or NetBeans (a Kotlin plugin would be needed). The great thing about IntelliJ is that the latest versions already come with a Kotlin plugin pre-installed, so the IDE is ready for Kotin development out of the box. This plugin already has a Kotlin compiler and also enables Kotlin syntax highlighting inside the code editor.

Another benefit of using IntelliJ is that it comes with a Java to Kotlin converter. If you have some Java code, or you are working in a mixed (both Java and Kotlin) project, you can convert Java code to Kotlin with the click of a button. Some minor changes might be needed in the resulting Kotlin code but in general, this converter works reasonably well.

IntelliJ has a paid for Ultimate version and a free Community version. The Express version has everything needed for Kotlin development and can be downloaded from the JetBrains website https://www.jetbrains.com/idea/download/. It's available for all platforms (Mac, Linux, and Windows).

Finally, if you are just trying out the syntax or just getting a feel for the language, there is a free online compiler and IDE available at https://try.kotlinlang.org/.

What's great about this online IDE is that it has plenty of examples of Kotlin features. There is also a section called Koans which has smaller code problems with the goal of teaching Kotlin. So, it might be worth checking it out.

Now that we have an IDE installed, let's get the first taste of Kotlin. Inside Intellij, start a new project and select Kotlin from the bar on the left and then select the Kotlin/JVM option, as seen in the following screenshot:

On the next screen, enter your project name and select KotlinJavaRuntime in the Use library section.

This will be enough to compile and run Kotlin code from the IDE. For smaller examples, setting up Maven or Gradle would be an overkill. 

Now that we have a Kotlin project, let's add some Kotlin code: the standard Hello World printed to the console. Add a file named Main.kt inside the src folder.

Inside that file add the following code:

fun main(args: Array<String>) {
println("Hello World!")
}

Now, there should be a little green icon in the gutter of the text editor and if you click it you should see "Hello World!" in the output.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.197.163