Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Hello World in Kotlin

Introduction to Kotlin

Kotlin is a modern programming language that is concise, expressive, and designed to be fully interoperable with Java. Developed by JetBrains, it has gained immense popularity, especially for Android development. In this tutorial, we will start with the simplest program in Kotlin: printing "Hello, World!" to the console.

Setting Up Your Environment

Before we can write our first Kotlin program, we need to set up our development environment. You can use several options to run Kotlin code:

  • IntelliJ IDEA: A powerful IDE that supports Kotlin natively.
  • Kotlin Playground: An online editor where you can write and run Kotlin code without installing anything.
  • Command Line: You can install the Kotlin compiler and run it from the command line.

For this tutorial, we will use the Kotlin Playground for simplicity.

Writing Your First Kotlin Program

Let's write the classic "Hello, World!" program in Kotlin. This program simply prints text to the console. Below is the code for our program:

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

In the code above:

  • fun: This keyword is used to declare a function in Kotlin.
  • main: This is the name of our function. It’s the entry point of the program.
  • println: This function prints the specified message to the console.

Running Your Program

If you are using Kotlin Playground, simply click on the "Run" button after typing the code. If you are using IntelliJ IDEA, you can run the program by right-clicking on the file and selecting "Run 'filename'". If you are using the command line, you can compile and run your program as follows:

kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
java -jar HelloWorld.jar

After running the program, you should see the following output:

Hello, World!

Conclusion

Congratulations! You have successfully written and executed your first Kotlin program. This simple exercise demonstrates the basic structure of a Kotlin application. From here, you can explore more advanced features of Kotlin, such as variables, control flow, and object-oriented programming.

Remember, practice is key to mastering any programming language. Keep experimenting with Kotlin and happy coding!