Kotlin Syntax

Kotlin Syntax

Let's look at a simple Kotlin program to understand its basic syntax and structure.

Basic Kotlin Program

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

Code Explanation

  1. fun: This keyword is used to declare a function in Kotlin. A function is a block of code designed to perform a particular task.
  2. main(): The main function is the entry point of every Kotlin program. When you run your code, execution always starts from the main function.
  3. println(...): This is a built-in function used to print text to the screen.
  4. No Semicolons!: Unlike Java, C++, or C#, Kotlin does not require semicolons (;) at the end of each statement.

Curly Braces {}

The curly braces {} mark the beginning and end of a block of code (like the body of the main function). Any code inside these braces belongs to that function.