Getting Started with Kotlin: Installation, Hello World, and Basic Syntax
This tutorial introduces Kotlin, explains its advantages over Java, guides you through setting up IntelliJ IDEA or Android Studio with the Kotlin plugin, and walks you through creating and running both a simple Hello Kotlin program and an object‑oriented example, while covering coding style recommendations.
Kotlin is a modern statically‑typed language that runs on the JVM and can be used for Android development. This chapter prepares you to learn and use Kotlin, and discusses how to run Kotlin programs in common development environments.
1.1 Introduction to Kotlin
Kotlin was launched by JetBrains in 2010 as a JVM‑based language designed to address many of Java's pain points, such as null‑pointer exceptions, by providing a type system that controls nullability.
As a cross‑platform language, Kotlin works in any Java environment, including server‑side applications, Android mobile apps, and desktop programs.
1.2 Advantages of Kotlin
Compared with Java, Kotlin offers cleaner syntax, better safety, and strong tooling support. It has no primitive types, fixed‑size arrays, and safe generics, and it supports closures and inline optimizations. Interoperability with Java is seamless: Kotlin can call Java code and vice‑versa.
1.3 Preparation
IntelliJ IDEA, provided by JetBrains, is the primary IDE for Kotlin development. Android developers can also use Android Studio, which is essentially IntelliJ IDEA with Android‑specific plugins. From IntelliJ 15 onward, the Kotlin plugin is bundled; Android Studio includes it from version 3.0.
To verify the plugin version, open the IDE preferences (Ctrl+, on Windows/Linux, ⌘+, on macOS), navigate to the Plugins section, browse repositories, search for "Kotlin", select "Kotlin" and "Kotlin Extension for Android" (now merged), and click Install. After installation, restart the IDE.
If network issues prevent download, use the provided download link or an offline mirror.
1.4 Hello Kotlin
1.4.1 First Kotlin Program
Create a new project in IntelliJ, enable the Kotlin (Java) option, and choose "Use library from plugin". Name the project (e.g., "Hello") and finish the wizard.
After the project builds, create a new Kotlin file named Hello.kt under the src folder.
The simple Hello Kotlin program consists of only three lines:
fun main(args: Array
) {
print("hello kotlin!")
}Run the program with Ctrl+R or the green run button.
Kotlin’s entry point is also a main() function, but unlike Java it does not require a surrounding class. Functions are declared with the fun keyword, and variable types follow the name after a colon.
1.4.2 Object‑Oriented Kotlin Program
Add a package and a Person class:
package com.kymjs.kotlin.helloobj
class Person(val name: String) {
fun printName() {
println(name)
}
}Update Hello.kt to import the class and use it:
import com.kymjs.kotlin.helloobj.Person
fun main(args: Array
) {
println("hello kotlin!")
Person("Zhang Tao").printName()
}Run the program again; note that object creation does not require the new keyword and that the compiler assists with import statements.
1.5 Coding Style
Follow Java‑like conventions: use camelCase, capitalize class names, indent with four spaces, and avoid prefixes like m or underscores for properties. Place spaces around colons in type declarations, and format lambda expressions with spaces around braces, arrows, and parameters.
interface Foo
: Bar {
fun foo(a: Int): T
}1.6 Summary
In this chapter you wrote two Kotlin programs—a simple Hello World and an object‑oriented example—gaining hands‑on experience with Kotlin’s concise syntax and IDE setup. Continue to the next chapter to explore Kotlin’s full language features.
Hujiang Technology
We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.