Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single exclamation mark in Kotlin

Tags:

kotlin

People also ask

What does single exclamation mark mean in Kotlin?

It's recognised by the exclamation mark at the end (e.g. String! ) and in essence just means that it's a type that Kotlin doesn't know if it's nullable or not (since Java doesn't have this built-in).

What is the question mark in Kotlin?

(Common source) (JVM source) (JS source) (Native source) true if this type was marked nullable in the source code. For Kotlin types, it means that null value is allowed to be represented by this type. In practice it means that the type was declared with a question mark at the end.


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


It's the notation for platform types:

T! means "T or T?"


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.


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.


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?.