Kotlin, a modern statically-typed programming language, has gained significant traction in the developer community, particularly for Android app development. Its concise, expressive, and safe syntax has made it a popular choice for many. In this article, we'll delve into the core syntax of Kotlin, helping you understand its structure and features.
Kotlin Basics: Variables and Data Types
Kotlin introduces variables with the `var` keyword for mutable variables and `val` for immutable (constant) ones. It's a statically-typed language, meaning you must declare the type of a variable. However, Kotlin also supports type inference, allowing you to omit the type declaration if the compiler can infer it.
Here's how you declare variables in Kotlin:

var name: String = "John Doe"val age: Int = 30val isStudent: Boolean = falseval pi = 3.14// Type inference
Functions: Defining and Calling
Functions in Kotlin are defined using the `fun` keyword. They can be defined with or without a return type. Kotlin supports default arguments and named arguments, making function calls more flexible.
Here's a simple function definition and call:
```kotlin fun greet(name: String = "World", greeting: String = "Hello"): String { return "$greeting, $name!" } fun main() { println(greet(greeting = "Hi")) // Outputs: Hi, World! } ```
Control Structures: Conditional and Looping
Kotlin uses `if`, `when` (similar to switch in other languages), `for`, and `while` for control flow. It also introduces `when` as an expression, allowing it to be used where any expression is expected.

Here's an example of `when` as an expression:
```kotlin fun getGreeting(hour: Int) = when { hour < 12 -> "Good morning" hour < 18 -> "Good afternoon" else -> "Good evening" } ```
Classes and Objects: Defining and Instantiating
Kotlin supports classes and objects, with the `class` keyword defining a class and `object` defining a singleton object. Kotlin also introduces data classes, which provide boilerplate code for common tasks like `equals()`, `hashCode()`, and `toString()`.
Here's a simple data class definition:

```kotlin data class Person(val name: String, val age: Int) ```
Extensions and Lambdas: Functionality on the Fly
Kotlin allows you to extend any class with new functionality using extensions. It also supports lambda expressions, enabling you to pass functions as arguments.
Here's an example of an extension function and a lambda:
```kotlin fun String.greet() = println("Hello, $this!") fun main() { listOf("Alice", "Bob", "Charlie").forEach { name -> name.greet() } } ```
Null Safety: Kotlin's Standout Feature
Kotlin introduces null safety, preventing null pointer exceptions at compile time. You must explicitly declare if a variable can hold a null value using the `?` operator. Kotlin provides safe calls (?.), elvis operator (?,), and null checks (!!) to handle null values.
Here's an example of null safety in Kotlin:
```kotlin var name: String? = "Alice" println(name?.length ?: "No name provided") // Safe call and elvis operator if (name != null) { // Null check println(name.length) } ```



















