Kotlin, a modern statically-typed programming language, has gained significant traction in the Android development community due to its concise syntax, improved performance, and better interoperability with Java. If you're new to Kotlin or looking to enhance your skills, this article will provide you with practical Kotlin code examples to help you understand and apply its features effectively.
Getting Started with Kotlin
Before diving into code examples, ensure you have the Kotlin plugin installed in your Integrated Development Environment (IDE). For IntelliJ IDEA, you can install it via the Settings > Plugins menu. Once installed, create a new Kotlin project or add Kotlin support to an existing one.
Hello, World! in Kotlin
A classic starting point for any programming language is the "Hello, World!" example. Here's how you can achieve this in Kotlin:

```kotlin fun main() { println("Hello, World!") } ```
In this example, `fun main()` defines the entry point of the application, and `println()` prints the given string to the standard output.
Kotlin Basics
Variables and Data Types
Kotlin is a statically-typed language, meaning you must declare the type of a variable when you create it. However, Kotlin also supports type inference, allowing you to omit the type declaration if the compiler can infer it. Here's how you can declare variables in Kotlin:
```kotlin val immutableVariable: Int = 10 // Explicit type declaration var mutableVariable = 20 // Type inference ```
Control Structures
Kotlin offers various control structures, such as if-else expressions and loops (for, while, do-while). Here's an example of an if-else expression with type inference:

```kotlin val age = 18 val message = if (age >= 18) "You can vote!" else "Sorry, you cannot vote yet." println(message) ```
Kotlin Functional Programming
Kotlin supports functional programming constructs, making it easy to write concise and expressive code. Here are a few examples:
Lambdas
Lambdas allow you to pass functions as arguments to other functions. Here's an example of sorting a list of strings using a lambda:
```kotlin val names = listOf("Alice", "Bob", "Charlie") val sortedNames = names.sortedBy { it.length } println(sortedNames) ```
Higher-Order Functions
Kotlin provides several higher-order functions, such as `map()`, `filter()`, and `reduce()`, which allow you to perform operations on collections. Here's an example of using `map()` and `filter()` to transform and filter a list of integers:

```kotlin val numbers = listOf(1, 2, 3, 4, 5) val squaredEvenNumbers = numbers.filter { it % 2 == 0 }.map { it * it } println(squaredEvenNumbers) ```
Kotlin Classes and Objects
Kotlin introduces several features that make working with classes and objects more enjoyable. Here's an example of a simple Kotlin class with properties, methods, and a constructor:
```kotlin class Person(val name: String, var age: Int) { fun celebrateBirthday() { age++ } } fun main() { val person = Person("Alice", 30) person.celebrateBirthday() println("Happy ${person.age}th birthday, ${person.name}!") } ```
Data Classes
Data classes are a special kind of class that provides boilerplate code for common use cases, such as equals(), hashCode(), and toString(). Here's an example of a data class:
```kotlin data class User(val id: Int, val name: String) fun main() { val user1 = User(1, "Alice") val user2 = User(1, "Alice") println(user1 == user2) // Prints: true } ```
Kotlin Coroutines
Kotlin coroutines are a powerful way to write asynchronous, non-blocking code using sequential, easy-to-read syntax. Here's a simple example of a coroutine that delays execution for a specified duration:
```kotlin import kotlinx.coroutines.* fun main() = runBlocking { launch { delay(2000L) // Delays for 2 seconds println("World!") } println("Hello,") } ```
This article has provided you with a solid foundation of Kotlin code examples, covering basics, functional programming, classes, and coroutines. As you continue your Kotlin journey, explore the official Kotlin documentation (kotlinlang.org/docs/home.html) for more in-depth information and advanced topics.






















