Sling Academy
Home/Kotlin/A Quick Tour of Kotlin Syntax

A Quick Tour of Kotlin Syntax

Last updated: November 29, 2024

Introduction

Kotlin, a statically-typed programming language targeted at the JVM and Android, is designed to interoperate fully with Java while also offering various features and enhancements for safer and more concise code. This guide will take you through the basic syntax of Kotlin.

Variables

In Kotlin, you can define variables using two keywords: val for read-only variables and var for mutable ones.

val name: String = "John Doe" // Immutable variable
var age: Int = 25 // Mutable variable

Note that Kotlin can infer types, so the type declarations are optional:

val name = "John Doe"
var age = 25

Functions

Function declarations in Kotlin start with the keyword fun. Here is a simple example:

fun greet(name: String): String {
    return "Hello, $name!"
}

If you want to make your function even shorter, you can use the single-expression function syntax:

fun greet(name: String) = "Hello, $name!"

Basic Control Flow

Kotlin supports the usual set of control flow constructs such as if, when, for, and while loops.

If Expression

The if construct is an expression and can return a value:

val max = if (a > b) a else b

When Expression

The when construct in Kotlin can be used as a replacement for switch-case. Here's an example:

val x = 3
val result = when (x) {
    1 -> "One"
    2, 3 -> "Two or Three"
    else -> "Unknown"
}

Loops

Kotlin provides various ways to create loops. Here are some common examples:

For Loop

You can iterate over ranges and collections effortlessly using the for loop:

for (i in 1..5) {
    println(i)
}

While Loop

The while loop functions similarly to other languages:

var x = 5
while (x > 0) {
    println(x)
    x--
}

Conclusion

This quick tour provides just a glimpse of Kotlin's concise and expressive syntax. As you explore further, you'll find more exciting features such as extension functions, null safety, and coroutines that make Kotlin a powerful tool in your programming arsenal.

Next Article: Setting Up Kotlin on the Command Line

Previous Article: Understanding Kotlin’s Code Structure

Series: Basics of Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin