Kotlin, Nulls, and the Double Exclamation Mark: An Introduction
\n\nHave 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).
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.
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\nThe Problem !! Solves: Null Safety in Kotlin
\n\nKotlin 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.
var name: String = "Alice"\nname = null // Compilation error!\n\n\nBut 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?.
var nullableName: String? = "Bob"\nnullableName = null // Now it's allowed\n\n\nAnd here's where the magic begins. You can't just call a method on a nullable variable:
\n\nval length = nullableName.length // Error! "Only safe (?.) or non-null asserted (!!.) calls are allowed"\n\n\nKotlin 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.
What is the !! (Double Bang) Operator?
\n\nThe !! 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.
Syntactically, it looks like two exclamation marks after the variable:
\n\nval nonNullValue = nullableValue!!\n\nHow It Works with an Example
\n\nImagine 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.
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\nIn 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\nWhen to Use !! (And When Absolutely Not)
\n\nThe !! (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\nWhen You Can (and Should) Use !!
\n\n- \n
- 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