Skip to main content
Kotlin Basics
CHAPTER 03 Beginner

Kotlin Syntax and First Program

Updated: May 18, 2026
5 min read

# CHAPTER 3

Kotlin Syntax and First Program

1. Chapter Introduction

Every programming journey begins with a "Hello, World!" application. In this chapter, we will write our first Kotlin program and dissect it line by line. We will learn how Kotlin uses functions, how to format output, and learn the rules of Kotlin's syntax, specifically the delightful absence of semicolons!

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Write and execute a basic Kotlin program.
  • Understand the role of the main function.
  • Print text to the console using print() and println().
  • Write single-line and multi-line comments.
  • Appreciate Kotlin's concise syntax compared to Java.

3. The "Hello, World!" Program

Let's create a file. In IntelliJ, right-click the src folder -> New -> Kotlin Class/File. Name it Main.kt (select File).

Type the following code exactly:

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

To run it, click the small green "Play" triangle next to fun main() in IntelliJ. *Output:*

1
Hello, World!

4. Code Breakdown

Let's break this down step-by-step:

#### A. The fun Keyword In Kotlin, fun stands for "function". It is used to declare a new function (a block of code designed to do a specific job).

#### B. The main Function main is a special name. It is the entry point of every Kotlin application. When the JVM starts your program, it specifically searches for a function named main and executes the code inside it first.

#### C. Curly Braces {} The curly braces {} define a "block" or "scope" of code. Everything between the opening { and the closing } belongs to the main function.

#### D. The println() Function println() prints the text inside the parentheses to the screen and automatically moves the cursor to a new line at the end. (If you use print() instead, it does not add a new line).

#### E. No Semicolons! Notice there is no semicolon ; at the end of the println statement! Unlike Java, C++, or C#, Kotlin infers the end of a statement via line breaks. You *can* use semicolons if you put two statements on the same line, but it is considered bad practice.

5. Comments in Kotlin

Comments are notes for yourself or other developers. The compiler completely ignores them.

Single-line comments start with //:

kotlin
1234
// This is a comment. It explains the code below.
fun main() {
    println("Hello") // This is an inline comment
}

Multi-line comments (block comments) use /* and */:

kotlin
12345678
/* 
   This is a multi-line comment.
   It can span across several lines.
   Useful for disabling large chunks of code during testing.
*/
fun main() {
    println("Welcome")
}

6. Print vs Println

Let's see the difference between print and println.
kotlin
123456
fun main() {
    print("One ")
    print("Two ")
    println("Three")
    println("Four")
}

*Output:*

12
One Two Three
Four

Notice how "One", "Two", and "Three" end up on the same line, but "Four" is pushed to the next line because the println on "Three" added a line break.

7. Mini Project: Simple Console Layout

Let's combine what we know into a small profile printer.
kotlin
123456789101112131415
fun main() {
    // Print a header
    println("======================")
    println("   DEVELOPER PROFILE  ")
    println("======================")
    
    // Print data
    println("Name: Kotlin Coder")
    println("Role: Android Developer")
    
    /* 
       End of program.
       The JVM will terminate the app now.
    */
}

8. Common Mistakes

  • Capitalizing fun: Kotlin is case-sensitive. Fun main() or FUN main() will cause a compiler error. Keywords must be lowercase.
  • Using single quotes for strings: In Kotlin, "Hello" (double quotes) is a String (text). 'A' (single quotes) is a single Character. You cannot use single quotes for multiple letters!

9. Best Practices

  • Indentation: Standard Kotlin style uses 4 spaces for indentation inside a block {}. IntelliJ will format this automatically if you press Ctrl + Alt + L (Windows) or Cmd + Option + L (Mac).
  • Omit Semicolons: Never use semicolons at the end of lines. Kotlin developers actively dislike seeing them in code.

10. Exercises

  1. 1. Create a Main.kt file.
  1. 2. Write a main function.
  1. 3. Use println() to output your favorite movie, book, and food on three separate lines.
  1. 4. Add a single-line comment explaining what the code does.

11. MCQs with Answers

Question 1

What is the entry point of a Kotlin executable program?

Question 2

What keyword is used to declare a function in Kotlin?

Question 3

What is the difference between print() and println()?

Question 4

Are semicolons required at the end of statements in Kotlin?

Q5. How do you write a single-line comment in Kotlin? a) # Comment b) <!-- Comment --> c) // Comment Answer: c) // Comment.
Question 6

Which brackets define a block of code (like a function body)?

Q7. Is Kotlin case-sensitive? a) Yes (fun and Fun are different) b) No Answer: a) Yes.

Q8. Can you use single quotes ' ' to define a string of words (e.g., 'Hello World') in Kotlin? a) Yes b) No, single quotes are strictly for single Characters Answer: b) No, single quotes are for single Characters.

Q9. Prior to Kotlin 1.3, main required an array parameter (fun main(args: Array<String>)). Is this still strictly required? a) Yes b) No, you can now write fun main() without parameters if you don't need command line arguments. Answer: b) No, the parameterless main is now standard.

Question 10

What IDE shortcut automatically formats your Kotlin code correctly?

12. Interview Questions

  • Q: Compare the "Hello World" syntax of Java and Kotlin. What boilerplate code does Kotlin remove? (Answer: Kotlin removes the mandatory enclosing class wrapper, the public static modifiers, the args array parameter, and semicolons).
  • Q: Why did the creators of Kotlin choose to make semicolons optional?

13. Summary

Kotlin's basic syntax is remarkably clean and highly readable. By eliminating unnecessary boilerplate like class wrappers for the main function and trailing semicolons, Kotlin allows developers to focus on the logic rather than the scaffolding. Comments help document the code, while println provides a simple way to output data to the user.

14. Next Chapter Recommendation

Printing hardcoded text is great, but programming is about managing dynamic data. In Chapter 4: Variables and Data Types, we will learn how to store data in memory, explore Kotlin's strict Type System, and discover the critical difference between val and var.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·