Название: Programming Kotlin Applications
Автор: Бретт Мак-Лахлин
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119696216
isbn:
NOTE The “installation process” for IntelliJ on Mac OS X is pretty simple: just drag the package (presented as an icon) into your Applications folder. You'll then need to go to that folder and launch IntelliJ or drag the icon into your Dock, which is what I've done.
For Windows, you download the executable and run it. You can then create a shortcut on your desktop if you like.
In both cases, you can use the JetBrains Toolbox (which comes with the JetBrains Kotlin package) to keep your installation current and add updates when they're available.
You'll be given a pretty large number of options to get your IDE set up. For IntelliJ, you'll pick a UI theme (either is fine), a Launcher Script (I'd suggest you accept the default and let it create the script), the default plugins, and a set of featured plugins. You can click through these quickly, and then your IDE will restart. You'll see a welcome screen similar to Figure 1.3, and you should select Create New Project.
WARNING You may need advanced permissions to install the Launcher Script that IntelliJ creates if you accepted the default location on Mac OS X.
Be sure you select the Kotlin/JVM option when creating the project, as shown in Figure 1.4.
FIGURE 1.3 You'll generally be either creating a project from scratch or importing one from a code repository, like GitHub.
FIGURE 1.4 IntelliJ makes getting going in Kotlin simple and prompts you on creating a new project to include Kotlin libraries.
Create Your Kotlin Program
Once your project is up and running, create a new Kotlin file. Find the src/
folder in the left navigation pane, right-click that folder, and select New ➢ Kotlin File/Class (see Figure 1.5). You can enter the code from Listing 1.2, and it should look nice and pretty, as shown in Figure 1.6 (thanks IntelliJ!).
NOTE Your IDE may not be configured exactly like mine. If you don't see the src/
folder, you may need to click Project on the left side of your IDE to display the various folders, and possibly click again on the name of the project.
FIGURE 1.5 Kotlin code should go in the src/ folder.
NOTE From this point forward, code will typically not be shown in an IDE. That way, you can use the IDE of your choice (or the command line), because you should get the same results across IDEs.
FIGURE 1.6 IntelliJ automatically formats code and adds sensible syntax highlighting.
Compile and Run Your Kotlin Program
All that's left now is to compile and run the program yourself. This is easy, because IntelliJ gives you a convenient little green arrow to click when you have a Kotlin file with a main()
function defined. Just hover over the arrow and click (see Figure 1.7). You can then select Run and your filename (I named mine “UselessPerson”). Your program will be compiled and run, with the output shown in a new pane at the bottom of the IDE (see Figure 1.8).
FIGURE 1.7 You can click the green Run button and select the first option to build and run your code.
FIGURE 1.8 The empty output of your program (which will soon be non-empty) displays in its own window.
In this case, you shouldn't get any errors, but there's not any output either. We'll fix that shortly.
Fix Any Errors as They Appear
One last note before getting back to improving that useless Person
class. IntelliJ and all other IDEs are great at giving you visual indicators when there is a problem with your code. For example, Figure 1.9 shows IntelliJ once it's tried to compile the same program with an error. In this case, the open and close parentheses are missing from line 8. You'll see an orange indicator in the code editor and an error indicating line 8 (and column 20) in the output window.
You can then easily fix the error and rebuild.
Install Kotlin (and Use the Command Line)
For power users, there's a tendency to want to use the command line for nearly everything. Kotlin is no exception. Because it's “mostly Java” in the sense that it runs using a JVM and JDK, you can get pretty far without a lot of work.
Command-Line Kotlin on Windows
For Windows users, you'll first need a JDK. You can download one from the Oracle Java download page at www.oracle.com/technetwork/java/javase/downloads
. That download has version-specific instructions that are easy to follow.
Once you have Java, you need to get the latest Kotlin release from GitHub. You can find that at github.com/JetBrains/kotlin/releases/latest
(that link will redirect you
СКАЧАТЬ