The !! Operator in Kotlin: What It Is and How Not to Break Your Code

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Kotlin, Nulls, and the Double Exclamation Mark: An Introduction

\n\n

Have you ever written code in Kotlin, had the compiler complain about a potential null, and responded by putting two exclamation marks — !! — and moved on? If so, you're already familiar with the hero of our article — the double bang operator (also known as the !! operator in Kotlin).

\n\n

This symbol is both a lifesaver and a dangerous tool. It tells the compiler: "Shut up, I know what I'm doing, this variable is definitely not null." But if you're wrong — the application will crash with a NullPointerException. Sounds scary? In reality, it's just a powerful mechanism that you need to understand and use wisely.

\n\n

In this guide, we'll break down what double bang Kotlin is, how it works, where to use it, and where it's better to look for alternatives. You'll learn how to avoid shooting yourself in the foot and write reliable code.

\n\n

The Problem !! Solves: Null Safety in Kotlin

\n\n

Kotlin was created as a language that aimed to kill NullPointerException (NPE). And it almost succeeded. In Kotlin, types cannot be null by default. You simply cannot assign null to a variable of type String — the compiler won't allow it.

\n\n
var name: String = "Alice"\nname = null // Compilation error!\n
\n\n

But the real world is full of uncertainty. Data comes from databases, from users, from the network. So Kotlin introduced nullable types — types that can be null. They are denoted with a question mark: String?.

\n\n
var nullableName: String? = "Bob"\nnullableName = null // Now it's allowed\n
\n\n

And here's where the magic begins. You can't just call a method on a nullable variable:

\n\n
val length = nullableName.length // Error! "Only safe (?.) or non-null asserted (!!.) calls are allowed"\n
\n\n

Kotlin forces you to handle null. This is brilliant — you don't forget to check. But sometimes you know for sure that the value is not null. And that's when the double exclamation mark operator in Kotlin comes onto the scene.

\n\n

What is the !! (Double Bang) Operator?

\n\n

The !! operator in Kotlin is the non-null assertion operator. It takes a nullable value and converts it to a non-null type. If the value turns out to be null — a KotlinNullPointerException is thrown.

\n\n

Syntactically, it looks like two exclamation marks after the variable:

\n\n
val nonNullValue = nullableValue!!
\n\n

How It Works with an Example

\n\n

Imagine you have a function that looks up a user by ID. It returns User? because the user might not exist. But you know for sure that in your test environment, the user with ID = 1 exists.

\n\n
data class User(val name: String)\n\nfun findUser(id: Int): User? {\n    // Some search logic\n    return if (id == 1) User("Anna") else null\n}\n\nfun main() {\n    val user = findUser(1)\n    // We are sure user is not null\n    val userName = user!!.name\n    println(userName) // Outputs: Anna\n}\n
\n\n

In this example, double bang Kotlin worked correctly, and we got the name. But if the ID were 2, the application would crash with an exception.

\n\n

When to Use !! (And When Absolutely Not)

\n\n

The !! (double bang) operator is like a red button. You can press it, but only if you are 146% sure. Let's break down the legitimate scenarios.

\n\n

When You Can (and Should) Use !!

\n\n
    \n
  1. Interacting with Java code. Java doesn't know about nullable annotations. If a Java method returns an object, Kotlin sees it as a Platform Type (e.g., String!). You can handle it however you want. But e

Blogs

Book Recommendations