📝 Kotlin – Syntax: Understanding the Basics of Kotlin Code Structure
🧲 Introduction – Why Learn Kotlin Syntax First?
Before diving into advanced topics like functions, classes, and Android apps, it’s essential to understand Kotlin syntax—the foundational rules that define how Kotlin code is written and interpreted. Kotlin syntax is modern, clean, and less verbose than Java, making it easy for beginners and powerful for professionals.
🎯 In this guide, you’ll learn:
- The structure of a basic Kotlin program
- Syntax for variables, data types, functions, and control flow
- Kotlin’s approach to semicolons, type inference, and string templates
- How Kotlin syntax compares to Java
📄 Basic Kotlin Program Structure
fun main() {
println("Hello, Kotlin Syntax!")
}
🔍 Breakdown:
| Syntax Element | Description |
|---|---|
fun | Declares a function |
main() | Entry point of a Kotlin application |
println() | Prints output to the console |
{ ... } | Denotes the function body block |
✅ No need for a class or public static void main like in Java!
🧮 Kotlin Variables & Data Types
✍️ Syntax:
val language = "Kotlin" // Immutable
var year = 2025 // Mutable
🔍 Explanation:
| Keyword | Purpose |
|---|---|
val | Declares a read-only (immutable) variable |
var | Declares a mutable variable |
| Type Inference | Kotlin auto-detects types (String, Int, etc.) |
🔠 Kotlin Data Types Syntax
| Type | Syntax Example |
|---|---|
| String | val name = "Alex" |
| Integer | var age = 30 |
| Double | val pi = 3.14 |
| Boolean | val isOn = true |
| Char | val grade = 'A' |
Kotlin is statically typed, but you don’t always have to declare types explicitly.
📦 Kotlin Functions – Syntax
fun greet(name: String): String {
return "Hello, $name!"
}
fun– Keyword for defining functionsname: String– Parameter with type: String– Return type$name– String template (inline variable)
🎯 Kotlin Conditionals – Syntax
✅ if Statement:
val score = 85
if (score >= 80) {
println("Excellent")
} else {
println("Keep improving")
}
✅ when Expression:
val grade = "A"
when (grade) {
"A" -> println("Awesome!")
"B" -> println("Good")
else -> println("Needs Work")
}
🔁 Kotlin Loops – Syntax
🔄 for Loop:
for (i in 1..5) {
println(i)
}
🔄 while Loop:
var x = 3
while (x > 0) {
println(x)
x--
}
🧪 Kotlin String Templates
val user = "Sana"
val age = 28
println("Hello $user, you are $age years old.")
✅ Output:
Hello Sana, you are 28 years old.
Use $variable or ${expression} inside strings to embed values.
🚫 Kotlin Syntax Highlights (Dos and Don’ts)
| ✅ Good Syntax | ❌ Common Mistake |
|---|---|
val name = "Sam" | val name : "Sam" |
println("Hello") | print("Hello" (missing )) |
for (i in 1..5) | for i in 1..5: (Python style) |
No need for semicolon (;) | Avoid ending every line with ; |
📘 Kotlin vs Java – Syntax Comparison
| Concept | Kotlin | Java |
|---|---|---|
| Main Function | fun main() | public static void main(...) |
| Variable | val x = 5 | int x = 5; |
println("Hi") | System.out.println("Hi"); | |
| Function | fun greet(): String | String greet() { ... } |
📌 Summary – Recap & Next Steps
Kotlin syntax is minimal, expressive, and beginner-friendly. With fewer symbols and smarter defaults, you can focus on logic rather than boilerplate. Learning syntax fundamentals prepares you for advanced topics like classes, collections, and coroutines.
🔍 Key Takeaways:
- Use
valfor constants andvarfor variables. - Kotlin omits semicolons and offers type inference.
- Control flow (
if,when, loops) is elegant and concise. - String templates make formatting effortless.
⚙️ Practical Use:
Whether you’re writing scripts, backend logic, or Android UIs, understanding Kotlin syntax allows you to write readable, maintainable, and idiomatic code from day one.
❓ FAQs – Kotlin Syntax
❓ Does Kotlin require semicolons?
✅ No. Kotlin does not require semicolons at the end of each line.
❓ What is the difference between val and var?
✅ val is immutable (like final in Java), while var is mutable (can be reassigned).
❓ Can I declare a variable without specifying its type?
✅ Yes. Kotlin uses type inference. Example: val age = 25 automatically infers Int.
❓ How do I write a multi-line string in Kotlin?
✅ Use triple quotes:
val msg = """Line 1
Line 2
Line 3"""
❓ What is a string template in Kotlin?
✅ It’s a feature to embed variables directly into strings using $varName or ${expression}.
Share Now :
