Basic Syntax &
Structure
The building blocks every Android developer needs — from variables and control flow to null safety and functions.
Basic Syntax in Kotlin
The beauty of Kotlin lies in its simplicity. Let's look at a very basic example:
println("Hello, Kotlin!")
}
Notice something? There's no need to specify a return type when it's obvious. Kotlin infers types automatically — a feature known as type inference that reduces redundant code while keeping everything strongly typed.
Variables & Data Types
Variables in Kotlin are declared using val and var — one of Kotlin's smartest design decisions.
var age = 25 // mutable — can be updated
Kotlin supports data types such as Int, Double, Float, Boolean, Char, and String. Unlike older languages, Kotlin doesn't differentiate between primitive and wrapper types at the syntax level — everything behaves consistently.
Operators in Kotlin
Operators in Kotlin function similarly to most languages but with improved clarity.
Control Flow Statements
Control flow determines how code executes based on conditions.
if-else as an Expression
In Kotlin, if isn't just a statement — it's an expression that can return a value, reducing extra lines of code:
when Expression
The when expression is Kotlin's powerful alternative to the traditional switch statement — cleaner, safer, and more flexible:
1 -> println("Monday")
2 -> println("Tuesday")
else -> println("Other day")
}
Loops
Kotlin supports for, while, and do-while loops. The for loop with ranges is particularly elegant:
println(i)
}
// Prints 1 through 5 — no index management errors
Functions in Kotlin
Functions are declared using the fun keyword. String templates make them readable and expressive:
return "Hello, $name"
}
Kotlin also supports default parameters and named arguments, making APIs self-explanatory and reducing documentation dependency:
println("Hello, $name")
}
greet(name = "Alex") // Named argument
Comments
/* Multi-line
comment */
Null Safety in Kotlin
One of Kotlin's strongest features. In many languages, null pointer exceptions are common — Kotlin addresses this at the language level itself.
name?.length // Safe call operator
val length = name?.length ?: 0 // Elvis: default to 0
Conclusion
Unit 2 builds the foundation every Android developer needs. From variables and control flow to null safety and function declarations — these concepts form the structural skeleton of every application.
Kotlin's syntax is designed to reduce errors, improve readability, and speed up development without sacrificing power.
"Instead of fighting the language, you collaborate with it.
That's the real magic of Kotlin."

0 Comments
If you have any doubts, Please let me know