![]() ![]() Verbose language, lots of boilerplate code.The document linked above shows a case of String! being implicitly used, giving the example that item.substring(1), where item is a String! is allowed/type-safe but will fail at run-time if item evaluates to null. While simple to show with ints, as in this example, this issue is not strictly related to Java's int/Integer types. Int z = (int)x // kaboom *at run-time* if x is null! Integer y = x // "No OP" - all class types in Java are null-able If the Java code can return null, it is safest to put the value into a Type? immediately and then correctly deal with the null-ability.Ĭompare this to the Java code with similar run-time failure semantics: // Java Highly applicable to int-box- Integer in Java, although it is valid for any class type / value coming in from Java. ![]() It's thus designed for cases when it is known that the value "cannot" be null in a valid program, although the value comes from Java outside of the Kotlin type system. (For class implementation types, the NPE may delayed to usage: basically, Kotlin chooses to 'get out of the way'.) However, the assignment to z (Int) will fail at run-time if x evaluates to null. var x = JavaIntegerList.get(0) // Int! type inferred by Kotlin The following program is accepted as type-safe, as Kotlin allows the type of the x/Int! expression to be used as "Int or Int?". I found the examples in the linked question underwhelming. The Type! notation is called a Platform Type and is critical for Interoperability with the weaker-typed Java it means "the null-ability is unknown" and it allows for relaxed null-ability enforcement. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |