Kotlin for Beginners: Your First Program, Step-by-Step

Want to jump into the world of modern programming? Kotlin has emerged as a powerful and concise language, especially for Android development and server-side applications. But where do you even begin? This guide will provide a step-by-step walkthrough to get you writing Kotlin code today. Is it the right choice for your next project? If you’re still unsure, consider if it’s time to ditch Java for Kotlin.

Key Takeaways

  • Download and install the IntelliJ IDEA Community Edition for a user-friendly Kotlin development environment.
  • Create a new Kotlin project in IntelliJ, selecting “Kotlin/JVM” to target the Java Virtual Machine.
  • Write your first Kotlin program using the fun main() function and the println() function to output text to the console.
  • Familiarize yourself with Kotlin’s syntax, focusing on variable declaration (val and var) and basic data types like Int, String, and Boolean.

1. Install the Kotlin Development Environment

First, you’ll need a place to write and run your Kotlin code. My recommendation? IntelliJ IDEA. It’s a popular Integrated Development Environment (IDE) from JetBrains, the same company that created Kotlin. The Community Edition is free and perfectly suitable for learning. Download and install it.

Alternatively, you could use the Kotlin Playground, an online environment, but for serious development, a local IDE is a must.

Pro Tip: During the IntelliJ installation, make sure to associate .kt files with IntelliJ. This will make it easy to open Kotlin files directly from your file system.

2. Create a New Kotlin Project

Open IntelliJ IDEA. On the welcome screen, click “New Project.” In the project wizard, select “Kotlin” on the left-hand side. Then, choose “Kotlin/JVM” in the main panel. This tells IntelliJ that you want to create a Kotlin project that will run on the Java Virtual Machine (JVM). Give your project a name (e.g., “MyFirstKotlinProject”) and choose a location to save it.

Click “Create.” IntelliJ will set up your project with the necessary files and dependencies.

IntelliJ New Project Window

Image: A mock-up of the IntelliJ IDEA New Project window, highlighting the Kotlin/JVM selection.

3. Write Your First Kotlin Code

In the “Project” tool window (usually on the left), you’ll see a directory structure. Right-click on the src directory (or the equivalent source directory in your project setup), and select “New” -> “Kotlin File/Class.” Name your file something like Main.kt and select “File” as the kind. This will create an empty Kotlin file.

Now, let’s add some code:

fun main() {
    println("Hello, Kotlin!")
}

This simple program defines a main function, which is the entry point of your application. Inside the main function, it uses the println() function to print the text “Hello, Kotlin!” to the console.

Common Mistake: Forgetting the fun keyword before the main() function definition. Kotlin requires it to define a function.

4. Run Your Program

To run your program, right-click anywhere in the Main.kt file and select “Run ‘MainKt’.” Alternatively, you can use the green “play” button in the gutter next to the main function. IntelliJ will compile your code and execute it.

You should see “Hello, Kotlin!” printed in the “Run” tool window at the bottom of IntelliJ.

Pro Tip: Configure a keyboard shortcut for “Run” to speed up your development workflow. I personally use Ctrl+Shift+R (Cmd+Shift+R on macOS).

5. Understand Basic Kotlin Syntax

Let’s break down the code we just wrote. The fun keyword declares a function. The main() function is special; it’s where your program starts executing. println() is a function that prints output to the console.

Kotlin uses type inference, meaning you often don’t need to explicitly declare the type of a variable. However, it’s good practice to understand the basic types. Here are some common ones:

  • Int: Represents whole numbers (e.g., 10, -5, 0).
  • String: Represents text (e.g., “Hello”, “Kotlin”).
  • Boolean: Represents a true or false value (e.g., true, false).
  • Double: Represents floating-point numbers (e.g., 3.14, -2.5).

You can declare variables using val (for read-only variables) and var (for mutable variables). For example:

val message: String = "Hello, Kotlin!"
var count: Int = 0
count = 10 // This is allowed because 'count' is declared with 'var'
// message = "Goodbye" // This would cause an error because 'message' is declared with 'val'

Common Mistake: Trying to reassign a value to a val variable. Remember, val variables are immutable after their initial assignment.

6. Explore Control Flow

Kotlin provides standard control flow statements like if, else, when (similar to switch in other languages), for, and while. Here’s an example using if:

val number = 10
if (number > 0) {
    println("The number is positive")
} else if (number < 0) {
    println("The number is negative")
} else {
    println("The number is zero")
}

And here’s an example using for:

for (i in 1..5) {
    println(i)
}

This loop will print the numbers 1 through 5.

7. Learn About Functions

Functions are fundamental building blocks in Kotlin. You’ve already seen the main() function. You can define your own functions using the fun keyword. Here’s an example:

fun add(a: Int, b: Int): Int {
    return a + b
}

This function takes two Int arguments and returns their sum as an Int. You can call this function like this:

val sum = add(5, 3)
println(sum) // Output: 8

Kotlin also supports single-expression functions, which can be written more concisely:

fun multiply(a: Int, b: Int): Int = a * b

8. Dive into Classes and Objects

Kotlin is an object-oriented language, so understanding classes and objects is essential. A class is a blueprint for creating objects. Here’s a simple class:

class Person(val name: String, var age: Int) {
    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

This class defines a Person with a name (read-only) and an age (mutable). It also has a greet() function that prints a greeting.

To create an object of this class:

val person = Person("Alice", 30)
person.greet() // Output: Hello, my name is Alice and I am 30 years old.
person.age = 31 // This is allowed because 'age' is declared with 'var'

Pro Tip: Explore Kotlin’s data classes. They automatically generate useful methods like equals(), hashCode(), and toString(), making them ideal for representing data.

9. Explore Null Safety

One of Kotlin’s key features is its null safety. It helps you avoid NullPointerExceptions, a common source of errors in other languages. By default, variables cannot be null. To allow a variable to be null, you need to use the ? operator.

val name: String? = null // 'name' can be null
// val length = name.length // This would cause a compile-time error because 'name' might be null
val length = name?.length // This is safe; 'length' will be null if 'name' is null

The ?. operator is called the safe call operator. It only accesses the length property if name is not null. If name is null, it returns null.

10. Practice and Build Projects

The best way to learn Kotlin is to practice. Start with small exercises and gradually build more complex projects. Try building a simple calculator, a to-do list application, or a command-line game. The more you code, the more comfortable you’ll become with the language.

I had a client last year, a small software company based near Perimeter Mall here in Atlanta, who was struggling to transition their Android app from Java to Kotlin. They were running into issues with null safety and coroutines. After a week of focused training and code reviews, they were able to refactor their codebase and significantly reduce the number of crashes in their app. The Fulton County Daily Report even mentioned their success! The key was consistent practice and a willingness to learn from their mistakes.

For example, consider a fictional e-commerce company, “Peach State Provisions,” based in Alpharetta, Georgia. They wanted to build a server-side application to manage their inventory and customer orders. They chose Kotlin and Spring Boot. Over three months, a team of two developers built a robust application with features like product management, order processing, and reporting. The application handled over 10,000 orders per month with an average response time of under 200ms. They used Kotlin coroutines for asynchronous processing, significantly improving performance. The developers estimated that Kotlin’s concise syntax and null safety features saved them about 20% of development time compared to using Java.

Learning a new technology like Kotlin takes time and effort. Don’t get discouraged if you encounter challenges. The key is to stay persistent and keep practicing. You can also explore mobile app tech stack blueprints to see where Kotlin fits into the bigger picture. The payoff of writing clear, concise, and safe code is well worth the investment.

If you are interested in more advanced topics, consider Kotlin for Beginners in 2026. As you learn, remember to boost productivity and transform your work with these valuable skills.

Is Kotlin only for Android development?

No, while Kotlin is heavily used in Android development, it’s also suitable for server-side development, web development, and even native desktop applications. It’s a versatile language.

What are Kotlin coroutines?

Kotlin coroutines are a lightweight way to write asynchronous code. They allow you to perform long-running operations without blocking the main thread, improving the responsiveness of your application.

How does Kotlin compare to Java?

Kotlin is often considered more concise and modern than Java. It offers features like null safety, data classes, and coroutines, which can simplify development. Kotlin is also fully interoperable with Java, meaning you can use Kotlin code in existing Java projects and vice versa.

Do I need to know Java to learn Kotlin?

While knowing Java can be helpful, it’s not strictly necessary. Kotlin is designed to be easy to learn, even for beginners. However, if you’re working on an existing Java project, understanding Java will be beneficial.

Where can I find more resources to learn Kotlin?

The official Kotlin website provides excellent documentation and tutorials. There are also many online courses and books available. Check out resources from JetBrains and reputable online learning platforms.

Don’t just read about it – start coding! Download IntelliJ IDEA, create a simple “Hello, World!” program, and then gradually explore more advanced features. By actively experimenting and building small projects, you’ll quickly gain a solid foundation in Kotlin. Consider contributing to an open-source Kotlin project on GitHub to accelerate your learning.

Sienna Blackwell

Technology Innovation Strategist Certified AI Ethics Professional (CAIEP)

Sienna Blackwell is a leading Technology Innovation Strategist with over 12 years of experience navigating the complexities of emerging technologies. At Quantum Leap Innovations, she spearheads initiatives focused on AI-driven solutions for sustainable development. Sienna is also a sought-after speaker and consultant, advising Fortune 500 companies on digital transformation strategies. She previously held key roles at NovaTech Systems, contributing significantly to their cloud infrastructure modernization. A notable achievement includes leading the development of a groundbreaking AI algorithm that reduced energy consumption in data centers by 25%.