Null safety in kotlin9/11/2023 ![]() ![]() ![]() The way to leverage the existing null safety mechanism is by representing platform types as an actual Kotlin type (nullable or non-nullable). When we try to invoke Java code from Kotlin, null checks are relaxed due to platform types. The following mnemonic notation can be used to denote them: Hence, there is no syntax in the language to represent them. E.g.įrom the Kotlin compiler perspective, it is a type that can be nullable and non-nullable. if we try to declare a variable of a platform type, we will receive a compilation error. ![]() These types cannot be mentioned explicitly in the program, i.e. Java's declarations are treated as platform ( flexible) types in Kotlin. In this post, we will focus on how to take advantage of null safety when invoking Java code. Using an uninitialized this in a constructor (data inconsistency).Invoking external Java code, which in turn can throw NPEs.Nevertheless, we can still encounter NPEs by: Which means we can deal with null at compile time rather than bumping into a Null Pointer Exception. The JVM already provides a safety net in the form of bytecode verification, buffer overflow, type safety, etc., but Kotlin took this to a step further and baked null safety right into the type system. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |