Getting Started with IntelliJ IDEA
最近更新 | 2020-07-07 |
To get started, first download and install the latest version of IntelliJ IDEA.
Create an application
Once you've installed IntelliJ IDEA, it's time to create your first Kotlin application.
- In IntelliJ IDEA, select File | New | Project.
- In the panel on the left, select Kotlin.
-
Enter a project name, select Console Application as the project template, and click Next.
By default, your project will use the Gradle build system with Kotlin DSL.
-
Go through and accept the default configuration, then click Finish.
Your project will open. By default, you see the file
build.gradle.kts
, which is the build script created by the Project Wizard based on your configuration. It includes thekotlin("jvm")
plugin and dependencies required for your console application. -
Open the
main.kt
file insrc/main/kotlin
.
Thesrc
directory contains Kotlin source files and resources. Themain.kt
file contains sample code that will printHello World!
. -
Modify the code so that it requests your name and says
Hello
to you specifically, and not to the whole world.- Introduce a local variable
name
with the keywordval
. It will get its value from an input where you will enter your name –readLine()
. - Use a string template by adding a dollar sign
$
before this variable name directly in the text output like this –$name
.
fun main() { println("What's your name?") val name= readLine() println("Hello $name!") }
- Introduce a local variable
Run the application
Now the application is ready to run. The easiest way to do this is to click the green Run icon in the gutter and select Run 'MainKt'.
You can see the result in the Run tool window.
Enter your name and accept the greetings from your application!
恭喜! 你现在运行了第一个 Kotlin 应用程序。
What's next?
Once you’ve created this application, you can start to dive deeper into Kotlin syntax:
- Add sample code from Kotlin examples
- Install the EduTools plugin for IDEA and complete exercises from the Kotlin Koans course