Publicado · en mejora
Guía de Kotlin · 1/6
Por ahora, este capítulo solo está disponible en inglés.
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.
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.
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.
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:
build.gradle.kts.src/main/kotlin.main.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 rungradlew (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.
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.
kotlinc is handy for single-file experiments; Gradle is for real projects.build.gradle.kts) and run them with ./gradlew run.main is the entry point, compiled on the JVM into a class named after the file plus Kt.
0 comentarios
Iniciar sesión · Inicia sesión para dejar un comentario.
Sé el primero en comentar.