============================= Using Kotlin in IntelliJ IDEA ============================= Working with Kotlin from within IntelliJ IDEA is relatively straightforward. Remember that you can `get the full professional version`_ of this IDE to use for free on your own PC, but you will need to `apply for a student license`_. When you run IntelliJ, choose the option to create a new project, then make sure you've selected 'Kotlin/JVM' as the project type. .. figure:: newproj.png :scale: 75 % :align: center First page of IntelliJ project creation wizard The Kotlin REPL =============== #. To run the REPL, choose :menuselection:`Tools --> Kotlin --> Kotlin REPL` from the menu bar. This will open a new panel at the bottom of the IDE window, within which the REPL is running. Try entering a line of Kotlin code. Press :kbd:`Ctrl+Enter` to execute it. .. figure:: repl.png :scale: 75 % :align: center Kotlin REPL running within IntelliJ #. Now try some code that spans multiple lines: .. code-block:: kotlin var sum = 0 for (n in 1..10) { sum += n } You can run this loop by pressing :kbd:`Ctrl+Enter` after having entered the code for the loop body. To see the result, enter ``sum`` and press :kbd:`Ctrl+Enter` again. .. note:: One limitation of the REPL in IntelliJ is that repeated invocations of ``println()`` within the same block of code won't produce output on separate lines! Creating & Running Programs =========================== #. Right-click on the :file:`src` folder in the *Project* panel on the left of the IDE and choose :menuselection:`New --> Kotlin File/Class`. Enter ``hello`` as the name, leave the *Kind* field set to 'File' and click :guilabel:`OK`. #. In the resulting file :file:`hello.kt`, implement a simple program: .. code-block:: kotlin fun main(args: Array) { println("Hello World!") } #. To run this program, choose :menuselection:`Run --> Run...` from the menu bar, or press :kbd:`Alt+Shift+F10`. In the resulting pop-up menu, click on the 'HelloKt' option. The program will compile and run, and output will appear in the *Run* panel at the bottom of the IDE. Note that this will create a run configuration of the program. You can run it subsequently by clicking on the 'green triangle' in the toolbar at the top right, or by choosing :menuselection:`Run --> Run 'HelloKt'`, or by pressing :kbd:`Shift+F10`. #. Another thing you can do in IntelliJ is view the JVM bytecode generated for a particular Kotlin file. Try this now by opening :file:`hello.kt` and choosing :menuselection:`Tools --> Kotlin --> Show Kotlin Bytecode`. .. _get the full professional version: https://www.jetbrains.com/idea/download/ .. _apply for a student license: https://www.jetbrains.com/shop/eform/students