


Normally,types of String are not nullable. Kotlin types system differentiates between references which can hold null (nullable reference) and which cannot hold null (non null reference). Kotlin Nullable Types and Non-Nullable Types

Casting to non-null (with possible exception).I've demonstrated a number of strategies to convince the compiler that your code is okay: If you checked your local variable and it’s not null, nothing in the world could make it null, so Kotlin compiler allows to treat is as a non-null value. Then we use the helper function Grouping.eachCount() to transform that into Map.Ĭollection-level operations can be incredibly powerful and are often more useful than iterating over collections manually (especially because the standard library can optimize what's going on behind the scenes). Kotlin tries to provide null-safety even when the variable could be changed from another thread. What we're doing here is first converting our List into a Grouping. Here's how we might try to write it: fun countInstances(list: List): Map. Suppose we want to convert a List into a Map, where each Int represents the number of times each String appeared in the list. Code that you know is ironclad turns out to be full of potential nulls. This null safety can occasionally create some tricky situations, though. Try to use a nullable type in a non-null way and the compiler will yell at you. If you are a Java developer, you might have definitely.

One of the best features of Kotlin is its built-in null safety in the type system. Null Safety in Kotlin is to eliminate the risk of occurrence of NullPointerException in real time.
