Kotlin First Program 🎯

beginner
11 min

Kotlin First Program 🎯

Welcome to the exciting world of Kotlin! This tutorial is designed to guide you through your first Kotlin program. Whether you're a complete beginner or an intermediate learner, we've got you covered. Let's dive in!

What is Kotlin? 📝

Kotlin is a modern, statically-typed programming language for Android development and the JVM (Java Virtual Machine). It's designed to be concise, safe, and interoperable with Java.

Why Kotlin? 💡

Kotlin offers several benefits over Java, including null safety, extension functions, and coroutines. It's also adopted by Google as the preferred language for Android app development.

Installing Kotlin ✅

To get started, you'll need to install the Kotlin plugin for Android Studio. If you don't have Android Studio, download it from the official website. Once installed, open Android Studio and follow these steps:

  1. Open a new project.
  2. Select "Empty Activity" and click "Next".
  3. Name your application (e.g., "MyFirstKotlinApp") and click "Finish".
  4. In the main() function of the MainActivity.kt file, replace the Java code with Kotlin code.

Your First Kotlin Program 🎯

Here's a simple Kotlin program that prints "Hello, World!" to the console.

kotlin
fun main(args: Array<String>) { println("Hello, World!") }

Let's break it down:

  • fun main(args: Array<String>) defines a function named main that takes an array of strings as an argument. This is the entry point of the program.
  • println("Hello, World!") prints the string "Hello, World!" to the console.

Running Your Program ✅

To run your program:

  1. Click the green "Run" button in the Android Studio toolbar.
  2. Wait for the build to complete.
  3. Your "Hello, World!" message should now appear in the logcat window.

Congratulations! You've just written your first Kotlin program. 🎉

Next Steps 💡

Now that you've written your first program, let's explore some basic Kotlin concepts:

Data Types and Variables 📝

In Kotlin, there are several built-in data types, including:

  • Int (whole numbers)
  • Double (floating-point numbers)
  • String (text)
  • Boolean (true/false)

To create a variable, use the var keyword followed by the variable name, data type, and an equals sign (=).

kotlin
var myInt: Int = 10 var myDouble: Double = 3.14 var myString: String = "Kotlin" var myBoolean: Boolean = true

Operators 💡

Kotlin supports a variety of operators, including:

  • Arithmetic operators (+, -, *, /, %)
  • Comparison operators (==, !=, >, <, >=, <=)
  • Assignment operators (=, +=, -=, *=, /=, %=)

Here's an example using arithmetic and comparison operators:

kotlin
fun main(args: Array<String>) { var a = 10 var b = 5 println("a + b: ${a + b}") println("a - b: ${a - b}") println("a * b: ${a * b}") println("a / b: ${a / b}") println("a % b: ${a % b}") println("a > b: ${a > b}") println("a < b: ${a < b}") }

Control Structures 💡

Control structures allow you to control the flow of your program. In Kotlin, you can use if, else, when, and for loops.

Here's an example using the if and else statements:

kotlin
fun main(args: Array<String>) { var number = 10 if (number > 0) { println("The number is positive.") } else { println("The number is not positive.") } }

Quiz 💡

Quick Quiz
Question 1 of 1

What is the output of the following Kotlin code?

Conclusion ✅

You've now written your first Kotlin program and explored some basic concepts. Keep practicing and exploring Kotlin's features to become a proficient developer. Happy coding! 🚀


This tutorial is just the beginning of your Kotlin journey. In upcoming lessons, we'll dive deeper into Kotlin's features and best practices. Stay tuned! 🎯

This content is optimized for SEO and designed for beginners and intermediates.