Kotlin Syntax and First Program
# 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
mainfunction.
-
Print text to the console using
print()andprintln().
- 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 thesrc folder -> New -> Kotlin Class/File. Name it Main.kt (select File).
Type the following code exactly:
To run it, click the small green "Play" triangle next to fun main() in IntelliJ.
*Output:*
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 //:
Multi-line comments (block comments) use /* and */:
6. Print vs Println
Let's see the difference betweenprint and println.
*Output:*
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.8. Common Mistakes
-
Capitalizing
fun: Kotlin is case-sensitive.Fun main()orFUN 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 pressCtrl + Alt + L(Windows) orCmd + Option + L(Mac).
- Omit Semicolons: Never use semicolons at the end of lines. Kotlin developers actively dislike seeing them in code.
10. Exercises
-
1.
Create a
Main.ktfile.
-
2.
Write a
mainfunction.
-
3.
Use
println()to output your favorite movie, book, and food on three separate lines.
- 4. Add a single-line comment explaining what the code does.
11. MCQs with Answers
What is the entry point of a Kotlin executable program?
What keyword is used to declare a function in Kotlin?
What is the difference between print() and println()?
Are semicolons required at the end of statements in Kotlin?
Which brackets define a block of code (like a function body)?
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.
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
argsarray 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, whileprintln 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 betweenval and var.