This article explores different ways to convert a nullable Int (Int?) to an Int in Kotlin.

1. Using !! operator

The not-null assertion operator (!!) asserts that the receiver expression is not equal to null. It converts any value to a non-null type and throws NullPointerException if the value is null. You can use it to convert an Int? to an Int, as shown below:

Download Code

2. Using Elvis Operator

Instead of throwing a NullPointerException when the value is null, you can return a default value with an Elvis operator (?:). If the expression to the left of ?: is not null, the Elvis operator returns it; otherwise, it returns the expression to the right.

Download Code

3. Performing Null Check

Alternatively, you can perform an explicit null check on the Int? value and assign a default value when the value is null.

Download Code

 
You can also throw NPE if the value is null.

Download Code

That’s all about converting a nullable Int to an Int in Kotlin.