Writing your first Kotlin program is simple! Follow these steps to create, write, and run your first Kotlin program:
Step 1: Set Up Your Environment
To write Kotlin programs, you need a development environment. The recommended setup is IntelliJ IDEA, which is specifically designed for Kotlin development.
- Download and Install IntelliJ IDEA:
- Visit JetBrains’ IntelliJ IDEA.
- Download the Ultimate Edition (for Java/Kotlin development) or Community Edition (free version).
- Install it.
- Install Kotlin Plugin:
- Kotlin support is built into IntelliJ IDEA. If it’s not already installed, go to:
File → Settings → Plugins
(Windows/Linux) orIntelliJ IDEA → Preferences → Plugins
(Mac). - Search for “Kotlin” in the marketplace, and install it.
- Kotlin support is built into IntelliJ IDEA. If it’s not already installed, go to:
Step 2: Create a New Kotlin Project
- Open IntelliJ IDEA and click on:
New Project
.
- Select Kotlin under “Languages” or “New Project Wizard.”
- Choose Kotlin/JVM as the project type.
- Configure your Project:
- Specify the project name (e.g., “FirstKotlinApp”).
- Choose or create a project location.
- Make sure you add a JDK (Java Development Kit). If not installed, download a JDK from AdoptOpenJDK or Oracle JDK.
- Click Finish to create the project.
Step 3: Write Your First Kotlin Code
- In the
src
folder, create a new Kotlin file:- Right-click
src → New → Kotlin File/Class
. - Name it something like
HelloWorld
.
- Right-click
-
Write your Kotlin code. Here’s your first program:
fun main() { println("Hello, Kotlin!") }
Step 4: Run Your Program
- Run the program by clicking the green play icon (▶️) next to the
main
function or at the top of the editor. - Alternatively, run it by right-clicking the file and selecting:
Run 'HelloWorldKt'
.
- You should see the output in the Run Tool Window at the bottom of the IDE:
Hello, Kotlin!
Understanding the Code
fun
: This keyword defines a function.main
: This is the entry point of the program, similar tomain
in Java.println
: A function that prints a line of text to the console.
Congratulations!
You’ve successfully written and executed your first Kotlin program! 🎉