Kotlin Syntax & Language Basics
Estimated reading: 3 minutes 60 views

📝 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 ElementDescription
funDeclares 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:

KeywordPurpose
valDeclares a read-only (immutable) variable
varDeclares a mutable variable
Type InferenceKotlin auto-detects types (String, Int, etc.)

🔠 Kotlin Data Types Syntax

TypeSyntax Example
Stringval name = "Alex"
Integervar age = 30
Doubleval pi = 3.14
Booleanval isOn = true
Charval 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 functions
  • name: 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

ConceptKotlinJava
Main Functionfun main()public static void main(...)
Variableval x = 5int x = 5;
Printprintln("Hi")System.out.println("Hi");
Functionfun greet(): StringString 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 val for constants and var for 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 :
Share

Kotlin – Syntax

Or Copy Link

CONTENTS
Scroll to Top