Kotlin for Android: syntax basics and features

Author: IT Sectr Published: 2026-02-10 Reading time: 8 min

Kotlin is a statically typed programming language from JetBrains, running on JVM and officially supported by Google for Android development since 2017. Kotlin is fully compatible with Java, has a concise syntax and built-in null safety. Kotlin Documentation — a complete guide to the language and standard library.

Key Takeaways

  • Kotlin — a statically typed JVM language from JetBrains with concise syntax and null safety
  • Null safety — the compiler distinguishes nullable (String?) and non-null (String) types, eliminating NullPointerException
  • Data classes — automatic generation of equals, hashCode, toString, copy and componentN
  • Extension functions — adding methods to existing classes without inheritance via static calls
  • Coroutines — asynchronous programming with sequential style through suspend functions without thread blocking

What is Kotlin?

Kotlin is a statically typed programming language created by JetBrains (the developer of IntelliJ IDEA) and released in 2016 under the Apache 2.0 license. Kotlin compiles to JVM bytecode, JavaScript and native code (via Kotlin/Native). Its main areas of application are Android development, server-side applications and cross-platform projects.

Google announced Kotlin as the official language for Android development at Google I/O 2017. According to Google (Android Developer Blog, 2026), more than 85% of new projects on Google Play use Kotlin. The language is also actively used in server-side development (Spring Boot, Ktor) and multiplatform projects (Kotlin Multiplatform Mobile).

Kotlin is fully compatible with Java — you can call Java code from Kotlin and vice versa within the same project. Build tools (Gradle, Maven) support both languages, allowing gradual migration from Java to Kotlin without development downtime.

Why Kotlin is More Popular than Java for Android

The main reasons: conciseness — Kotlin requires on average 40% fewer lines of code than Java for the same task. Null safety — the compiler prohibits using nullable types without checks. Functional capabilities — higher-order functions, lambdas, extension functions and coroutines make code more expressive and safer.

Syntax Basics

Basic Kotlin syntax combines conciseness with clarity. Variables are declared using val (immutable) and var (mutable). The compiler uses type inference, but explicit type declaration is supported. Functions are declared with the fun keyword.

kotlin
package com.example.intro

// Constants and variables
val appName: String = "KotlinApp"  // immutable
var version = 1.0  // mutable, type inferred

// Function with expression body
fun sum(a: Int, b: Int) = a + b

// String templates
fun greet(name: String) = "Hello, $name! Version ${appName}_$version"

// Default and named arguments
fun createUser(
    name: String,
    age: Int = 18,
    email: String? = null
)

The when expression replaces switch in Java and is more flexible: it supports type checks, ranges, conditions and branches without break. The compiler checks exhaustive coverage for enum and sealed classes. Smart casts automatically cast types after an is check, eliminating explicit casts.

kotlin
fun analyzeValue(value: Any) = when (value) {
    is String -> "String of length ${value.length}"  // smart cast
    is Int -> "Integer: $value"
    in 1..10 -> "Number in range 1-10"
    else -> "Unknown type"
}

Null Safety and Elvis Operators

Null safety is a key Kotlin feature that eliminates NullPointerException at compile time. In Kotlin, types cannot be null by default. To allow null, add ? to the type: String?. The compiler requires explicit checking of every nullable value.

kotlin
// Nullable and non-null types
val nonNull: String = "Always not null"
val nullable: String? = null  // allowed

// Safe call — executes only if not null
val length: Int? = nullable?.length

// Elvis operator — default value
val text: String = nullable ?: "Default text"

// Let — execute block with non-null value
nullable?.let { value ->
    println("Value: $value")
}

The safe call operator (?.) returns null if the object before the operator is null, and calls the method/property otherwise. The Elvis operator (?:) provides a default value if the left side is null. The not-null assertion (!!) throws a NullPointerException if the value is null — use it only when you are 100% sure the value exists.

Platform types — Java types from called Java code that Kotlin cannot mark as nullable or non-null. The type is displayed as String! (with an exclamation mark). The developer is responsible for null safety when working with Java code.

Data Classes and Destructuring

Data classes in Kotlin automatically generate equals(), hashCode(), toString(), copy() and componentN(). This eliminates the boilerplate of Java models (POJO) and makes code cleaner. Declaring a data class takes one line instead of 20-30 in Java.

kotlin
data class User(
    val id: Long,
    val name: String,
    val email: String,
    val age: Int? = null
)

// Usage
val user = User(1, "Anna", "anna@example.com")
val updatedUser = user.copy(age = 25)

// Destructuring
val (id, name) = user
println("User #$id: $name")

Destructuring allows decomposing a data class into variables in a single line. This is convenient when working with pairs, triples and other composite values. Destructuring is supported for data classes, standard collections (List, Map) and any class with operator fun componentN().

Extension Functions

Extension functions are a Kotlin mechanism for adding new methods to existing classes without inheritance. Extensions are compiled into static methods with a receiver parameter, so inheritance is not required and virtual dispatch is not supported.

kotlin
// Extension function for String
fun String.isEmail(): Boolean {
    return this.contains("@") && this.contains(".")
}

// Extension property
val String.isPhoneNumber: Boolean
    get() = this.matches(Regex("^\\+?[\\d-]{10,15}$"))

// Generic extension for collections
fun List<Int>.sumOfEvens(): Int {
    return this.filter { it % 2 == 0 }.sum()
}

// Usage
println("test@mail.com".isEmail())  // true

Extensions are widely used in Android SDK and libraries. For example, Activity extensions in Android KTX are extension functions for Activity, Fragment, View and other classes. Extensions can be declared as top-level functions in a file or as member extensions of a class.

Coroutines for Asynchronous Programming

Coroutines are an asynchronous programming mechanism in Kotlin that allows writing non-blocking code in a sequential style. A coroutine suspends execution at a suspend function without blocking the thread and resumes after the operation completes. This is an alternative to RxJava callback chains and AsyncTask.

kotlin
import kotlinx.coroutines.*

// Suspend function — can be paused
suspend fun fetchUserData(userId: Int): User {
    // delay — suspend function, does not block the thread
    delay(500)
    return User(userId, "User $userId", "user@example.com")
}

// Launch in coroutine
fun loadData() {
    CoroutineScope(Dispatchers.Main).launch {
        val user = fetchUserData(1)  // pause here
        updateUI(user)  // resume on Main thread
    }
}

Dispatchers determine the thread pool for a coroutine: Dispatchers.Main (UI thread), Dispatchers.IO (network/disk operations), Dispatchers.Default (CPU-intensive tasks). Structured concurrency ensures that a coroutine does not remain active after the scope completes — this prevents memory leaks.

kotlin
// Parallel execution via async/await
suspend fun loadDashboard(): Dashboard = coroutineScope {
    val user = async { fetchUserData(1) }
    val orders = async { fetchOrders(1) }
    val stats = async { fetchStats() }
    
    Dashboard(user.await(), orders.await(), stats.await())
}

Kotlin vs Java: Comparison

The choice between Kotlin and Java for a new Android project is a fundamental architectural decision. Kotlin offers a more modern syntax and built-in safety mechanisms, while Java has a wider pool of developers and mature libraries.

FeatureKotlinJava
Null safetyBuilt-in (nullable/non-null)Not built-in (@Nullable annotations)
Code volume−40% vs JavaBoilerplate (getters/setters)
Data classesdata class (one line)POJO with Lombok or manual
AsynchronyCoroutines (suspend/await)CompletableFuture, RxJava
Extension functionsBuilt-inOnly via utility classes
Smart castsAfter is-checkExplicit cast (Type) obj
Backward compatibility100% with Java

Performance of Kotlin and Java is comparable — both compile to JVM bytecode. Kotlin sometimes adds micro-overhead for coroutines and inlining, but this is negligible for mobile applications. Google and JetBrains are actively investing in Kotlin: Kotlin Multiplatform, Kotlin/Wasm and K2 compiler performance improvements.

Frequently Asked Questions

How is Kotlin different from Java?

Kotlin is 40% shorter than Java — less boilerplate. Kotlin adds null safety, data classes, extension functions, coroutines, smart casts, when expressions. Full compatibility with Java: both languages run on JVM and can be used in the same project without conflicts.

How does null safety work in Kotlin?

Types are non-null by default. For nullable use ?: String?. The compiler prohibits direct access to nullable values. Safe call (?.) returns null on null receiver. Elvis operator (?:) provides a default value. !! — force unwrap with a possible exception.

What are coroutines in Kotlin?

Coroutines are lightweight threads that allow writing asynchronous code sequentially. suspend functions pause without blocking the thread. launch and async start coroutines in a CoroutineScope. Dispatchers.IO for network requests, Dispatchers.Main for UI updates.

What are data classes in Kotlin?

data class automatically generates equals(), hashCode(), toString(), copy() and componentN(). Example: data class User(val name: String, val age: Int). Destructuring via val (name, age) = user. Similar to record in Java 14+, but with the additional copy() method.

How do extension functions work?

Extension functions add methods to existing classes: fun String.isEmail(): Boolean. The compiler replaces them with static calls with a receiver parameter. No inheritance required. Extensions are widely used in Android KTX: view.showToast(), activity.viewModels().

Summary

  • Kotlin — a statically typed JVM language from JetBrains, official Android language since 2017
  • Null safety — nullable/non-null types, safe call (?.), elvis (??), let-blocks for safe work with nullable
  • Data classes — automatic generation of equals, hashCode, toString, copy and destructuring
  • Extension functions — adding methods to any classes without inheritance, compiled to static calls
  • Coroutines — asynchronous code in sequential style with suspend functions, launch/async and Dispatchers
  • Comparison with Java — −40% code, null safety, smart casts, when expressions and 100% Java compatibility
  • Kotlin Multiplatform — Kotlin is used not only for Android but also for iOS (KMM) and servers (Ktor, Spring)

We will develop a mobile application turnkey

IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.

Discuss the project

Read also