For example the following codeįun main() class Person(var name: String = "", var surname: String = "")īut, in general, the use of !! operator it’s dangerous because expose our code to possible NullPointerException. The ?: operator, also known as Elvis operator, it’s like an if else operator.operator, also known as Safe calls, it’s used for calling a property, or a method, if and only if the field is not null. The !! operator mean that we assert that the property will never be null.Kotlin wayīefore explain how Kotlin is null safe we have to know some operators: Kotlin type system has distinguish two types of references that can hold null (nullable references) and those that can not (non-null references). This exception is thrown because we have initialized p1 with null and then we try to call a toString() method on null object. In Kotlin, nullability is intentionally treated to achieve null safety. combine is also an extension function which is called on a flow to combine with another flow as parameter and provides a lambda which sends values one from each flow. The code is syntactically correct and the compiler do not throw no kind of error at compile time, but what it happens if we try to run it? At runtime we have the exception NullPointerException. Kotlin combine two flows depending from each other, Combine many Kotlin flows to get just their current value.
0 Comments
Leave a Reply. |