Kotlin is a statically typed language from JetBrains. It most often runs on the JVM, compiling to the same bytecode as Java, so every Java library is available to you. It is also the preferred language for Android and is used for backends and multiplatform projects. This chapter shows three ways to run your first program: the browser-based Playground, the command-line compiler, and a Gradle project in IntelliJ IDEA.
Try it in the browser
The Kotlin Playground runs code without installing anything. You can switch the compile target (JVM, JS, Wasm and others), pick a compiler version, and share snippets by link. It is all you need while learning the syntax.
fun main() {
val name = "Kotlin"
println("Hello, $name!")
}
fun main() is the entry point. There is no wrapper class and no semicolons, and println comes from the standard library, so no import is needed.
The command-line compiler
With a JDK installed, kotlinc compiles a single file. SDKMAN! or Homebrew are the easiest installers on macOS and Linux; on Windows, download the archive from the Kotlin GitHub releases page and add its bin folder to your PATH.
# install (pick one)
sdk install kotlin
brew install kotlin
# compile and run
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar
-include-runtime bundles the Kotlin standard library into the JAR; without it, you would have to put the library on the classpath yourself. The bare compiler is great for understanding what happens under the hood, but real projects use a build tool.
IntelliJ IDEA
Since JetBrains created Kotlin, IntelliJ IDEA has the deepest support, and Kotlin tooling is included in the free edition. For Android work, use Android Studio, which is built on IntelliJ. To create a project:
- Choose New Project, then Kotlin as the language and Gradle as the build system.
- Select Kotlin as the Gradle DSL, so the build script is
build.gradle.kts. - Pick a JDK and click Create. A sample file appears under
src/main/kotlin. - Click the run icon in the gutter next to
main.
A Gradle project with the Kotlin DSL
You can also scaffold a project from the terminal. gradle init asks questions interactively, or you can answer them up front with flags:
mkdir hello-kotlin && cd hello-kotlin
gradle init --type kotlin-application --dsl kotlin
./gradlew run
gradlew (gradlew.bat on Windows) is the Gradle Wrapper. It downloads the Gradle version pinned by the project, so everyone on the team builds with the same one. The generated build script looks roughly like this:
// app/build.gradle.kts
plugins {
alias(libs.plugins.kotlin.jvm)
application
}
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
application {
mainClass.set("org.example.AppKt")
}
The Kotlin DSL lets you write build scripts in Kotlin itself. It does the same job as a Groovy build.gradle, but you get code completion and type checking in the IDE. The kotlin.jvm plugin compiles your sources, application adds the run task, and plugin and library versions live in the version catalog at gradle/libs.versions.toml.
Where main ends up
AppKt in mainClass is not a typo. On the JVM, top-level functions are compiled into static methods of a class named after the file plus Kt. So main in App.kt inside package org.example becomes org.example.AppKt.main.
// app/src/main/kotlin/org/example/App.kt
package org.example
fun main(args: Array<String>) {
if (args.isEmpty()) {
println("No arguments given")
} else {
println("First argument: ${args[0]}")
}
}
Pass arguments with ./gradlew run --args="hello world". If you don't need them, a plain fun main() works too.
Summary
- The Kotlin Playground lets you practice without installing anything.
kotlinc is handy for single-file experiments; Gradle is for real projects.- Start new projects with Gradle and the Kotlin DSL (
build.gradle.kts) and run them with ./gradlew run. - A top-level
main is the entry point, compiled on the JVM into a class named after the file plus Kt.
0 条评论
登录 · 登录后即可发表评论。