Kotlin Syntax
Kotlin syntax is clean, simple, and readable, making it easy for beginners to learn and use. Below are the basic syntax rules with clear examples.
1. Kotlin Program Structure
Every Kotlin program starts from the main() function.
-
fun→ keyword to define a function -
main()→ entry point of the program -
{ }→ code block
2. Statements & Semicolons
-
Semicolons (
;) are optional in Kotlin.
3. Variables in Kotlin
val (Read-only / Constant)
var (Mutable / Changeable)
4. Data Types
Kotlin automatically detects data types (type inference).
5. Comments
Single-line Comment
Multi-line Comment
6. Print Output
7. String Templates
Use $ to insert variables in strings.
8. If-Else Condition
Kotlin allows if as an expression.
9. When Statement (Switch Alternative)
10. Loops
For Loop
While Loop
11. Functions
Short form:
12. Null Safety (Very Important)
-
?allows null -
?.safe call operator
13. Class Syntax
Summary
-
Kotlin syntax is simple and expressive
-
Less boilerplate than Java
-
Built-in null safety
-
Beginner-friendly
