Single exclamation mark in Kotlin

Kotlin

Kotlin Problem Overview


What does a single exclamation mark mean in Kotlin? I've seen it a few times especially when using Java APIs. But I couldn't find it in the documentation nor on StackOverflow.

Kotlin Solutions


Solution 1 - Kotlin

They're called platform types and they mean that Kotlin doesn't know whether that value can or cannot be null and it's up to you to decide if it's nullable or not.

> In a nutshell, the problem is that any reference coming from Java may be null, and Kotlin, being null-safe by design, forced the user to null-check every Java value, or use safe calls (?.) or not-null assertions (!!). Those being very handy features in the pure Kotlin world, tend to turn into a disaster when you have to use them too often in the Kotlin/Java setting. > >This is why we took a radical approach and made Kotlin’s type system more relaxed when it comes to Java interop: now references coming from Java have specially marked types -- Kotlin Blog

Solution 2 - Kotlin

It's the notation for platform types:

T! means "T or T?"

Solution 3 - Kotlin

Platform Types

The type names or class names ending with single exclamation mark ! are called platform types in Kotlin. You find them when you are working in Kotlin with old Java code that doesn't contain nullability information.

Examples:

Nullable Information: Nullable Type

@Nullable String in Java is considered as String? by Kotlin.

Non-null Information: Non-null Type

@NotNull String in Java is considered as String by Kotlin.

No Information: Platform Type

String without annotations in Java is considered as String! by Kotlin.


How to deal with Platform Types?

You can work with a platform type either as a nullable or a non-null. The compiler will allow you to call all methods on this type. It’s your responsibility how to use them. If you know that the value can be null, you should compare it with null before you call methods on it. If you know it’s not null, you can use it directly but as in Java, you’ll get exception if your assumption about the nullability is wrong.

Note that you can't declare platform types in Kotlin code, they come only from Java code.


Inheritance and Platform Types

While overriding Java methods in Kotlin code, you have the option to declare parameters and return types as nullable or non-null. You need to choose this wisely, because if you decide to make the parameters non-null, the Kotlin compiler generates non-null assertions for these non-null parameters. And when next time you access this Kotlin code back from Java and you pass a null value, you'll get exception.

Hope that helps clearing all your doubts about Platform Types.

Solution 4 - Kotlin

A Type notated with ! is called platform type, which is a type coming from Java and thus can most probably be null. It’s what the Kotlin compiler infers by default when calling Java (for the most basic cases, Java methods can be annotated to get around this). You should handle platform types as nullable types, unless you certainly know that the particular API will never return null. The compiler allows platform types to be assigned to variables of both nullable and non-null types.

> Notation for Platform Types > > [...] > > T! means "T or T?" [...]

You could refer to platform types as "types of unknown nullability". Also important to know is that you cannot use the exclamation-marked type for your own types, it's not part of the Kotlin syntax, it's only a notation.

Solution 5 - Kotlin

> I've seen it a few times especially when using Java APIs

As mentioned by s1m0nw1, T! means T or T?. The next question is: what is T?? This is nicely documented at https://kotlinlang.org/docs/reference/null-safety.html. Kotlin does not allow certain elements to be null, e.g. String, unlike Java

> To allow nulls, we can declare a variable as nullable string, written > String?: > > var b: String? = "abc" > b = null // ok [...]

> b?.length > This returns b.length if b is not null, and null otherwise. The type of this expression is Int?.

Solution 6 - Kotlin

I use the funny interpretation to remember those things as below:

?: I dont know whether it is null or not.
!: Be careful! This might be null.
!!: Be careful, and yes I know it. This is always not null.

Solution 7 - Kotlin

Excerpt from Platform Types in Kotlin :

>Besides explicitly specifying a type as optional (e.g. Person?), Kotlin presents us with another beast, called Platform Type, specified by putting a single exclamation mark instead (e.g. Person!). This concept has been created for compatibility reasons, when accessing code from null-unsafe platforms like Java. It is often the case that when using a Java library, many methods return SomeType!, since the Kotlin compiler cannot infer if the result is nullable or not.

For example:

(Mutable)Collection<T>! 

Just means the following: "Java collection of T may be mutable or not, may be nullable or not".

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMibacView Question on Stackoverflow
Solution 1 - KotlinMibacView Answer on Stackoverflow
Solution 2 - KotlinvoddanView Answer on Stackoverflow
Solution 3 - KotlinYogesh Umesh VaityView Answer on Stackoverflow
Solution 4 - Kotlins1m0nw1View Answer on Stackoverflow
Solution 5 - Kotlinserv-incView Answer on Stackoverflow
Solution 6 - KotlinBrian VoView Answer on Stackoverflow
Solution 7 - KotlinAndy JazzView Answer on Stackoverflow