Kotlin Syntax Basics
Syntax means the rules a programming language uses. Just like English has grammar rules (capital letters, periods, question marks), Kotlin has its own set of rules. Breaking these rules causes errors. Understanding them helps you write clean, working code.
No Semicolons Required
Java requires a semicolon at the end of every statement. Kotlin does not. Each new line automatically ends the statement.
// Java style (semicolons required)
System.out.println("Hello");
int x = 5;
// Kotlin style (no semicolons needed)
println("Hello")
val x = 5You can use semicolons in Kotlin, but it is not recommended. Only use a semicolon when putting two statements on the same line:
val a = 1; val b = 2 // works but not recommendedCase Sensitivity
Kotlin treats uppercase and lowercase letters as different characters. myName, MyName, and MYNAME are three completely different identifiers.
val name = "Alice"
val Name = "Bob" // different variable from "name"
println(name) // Alice
println(Name) // BobKeywords
Keywords are reserved words that Kotlin uses for its own purposes. You cannot use them as variable or function names.
┌───────────────────────────────────────────────────┐
│ Common Kotlin Keywords │
├──────────┬──────────┬──────────┬──────────────────│
│ fun │ val │ var │ class │
│ if │ else │ when │ for │
│ while │ return │ true │ false │
│ null │ is │ in │ object │
│ import │ package │ this │ super │
└──────────┴──────────┴──────────┴──────────────────┘
Naming Conventions
Kotlin follows specific naming rules to keep code readable:
Variables and Functions: camelCase
val firstName = "Maria"
fun calculateTotal() { }Classes: PascalCase
class BankAccount { }
class UserProfile { }Constants: SCREAMING_SNAKE_CASE
const val MAX_SPEED = 120
const val APP_NAME = "MyApp"Indentation
Kotlin uses 4 spaces for each level of indentation. IntelliJ IDEA handles this automatically when you press Enter inside a code block.
fun main() {
if (true) {
println("First level inside main")
if (true) {
println("Second level deep")
}
}
}Proper indentation does not affect how the program runs, but it makes code much easier to read.
Code Blocks
A code block is everything between { and }. Functions, loops, and conditions all use code blocks.
Opening brace starts the block
│
▼
fun greet() {
println("Hello") ← code lives inside here
println("Welcome")
}
▲
│
Closing brace ends the block
Importing Packages
Kotlin includes built-in tools organized into packages. Most common tools are available by default. For additional tools, you add an import statement at the top of your file.
import kotlin.math.sqrt
import java.util.Date
fun main() {
println(sqrt(16.0)) // 4.0
}Package Declaration
Large projects organize files into packages. A package is like a folder with a label. You declare the package at the very top of the file:
package com.myapp.utils
fun formatName(name: String): String {
return name.trim()
}For small beginner projects, you do not need to declare a package.
Single Expression Functions
When a function only does one thing, Kotlin allows a shortcut syntax using = instead of a full code block:
// Standard function
fun double(x: Int): Int {
return x * 2
}
// Single expression function (same result)
fun double(x: Int): Int = x * 2Both versions do exactly the same thing. The second version is shorter and preferred when the logic fits in one line.
