Kotlin Syntax, Variables, Data Types, and Operators
Learn Kotlin basics in a strong foundation: variables, val vs var, primitive-like types, strings, interpolation, and everyday operators.
Inside this chapter
- Variables and Immutability
- Core Data Types
- Strings and Interpolation
- Arithmetic and Comparison Operators
- Type Conversion
- Real-World Example
Series navigation
Study the chapters in order for the clearest path from Kotlin setup and syntax to coroutines, backend work, clean design, multiplatform thinking, and advanced engineering practice. Use the navigation at the bottom to move smoothly through the full tutorial series.
Variables and Immutability
val appName = "ShopSmart"
var retryCount = 0
val creates a read-only reference, while var allows reassignment. This matters because immutability reduces accidental bugs and makes program flow easier to reason about.
Core Data Types
Int,Long,Double,FloatBooleanCharString
val age: Int = 24
val price = 499.99
val enabled = true
Kotlin has type inference, so the compiler can often determine types automatically. This keeps code shorter without making it vague.
Strings and Interpolation
val user = "Asha"
val points = 120
println("User $user has $points reward points")
String interpolation makes code more readable than manual concatenation and is used constantly in logging, UI messaging, diagnostics, and generated output.
Arithmetic and Comparison Operators
val total = 8 + 2
val discount = total - 1
val isLarge = total > 5
val isEqual = total == 10
Students should be comfortable with arithmetic, assignment, comparison, and logical operators because they appear in almost every program.
Type Conversion
val count = 10
val asLong = count.toLong()
val asString = count.toString()
Kotlin avoids many implicit conversions. This makes data transformation more explicit and reduces hidden behavior.
Real-World Example
val itemPrice = 799
val tax = 80
val finalPrice = itemPrice + tax
println("Payable amount: $finalPrice")
This kind of simple calculation grows naturally into business-rule modeling in e-commerce, finance, logistics, or analytics systems.