Christoph Loy
12/02/2024, 8:31 AMval x: String? = foo()
x?.length
Is this supported behavior in Kotlin? If yes, is this documented somewhere? Or just an implementation detail?Kai Yuan
12/02/2024, 1:04 PMreturns null anywaysor return nullable String anyway? If you know the return value from the Java side won't be null, you can consider adding:
val x = checkNotNull(foo()){"Null detected although @NonNull on the Java side"}
Robert Williams
12/02/2024, 1:34 PMx : String =
(maybe you don’t even need to set it explicitly because it should be inferred from the NonNull annotation)Robert Williams
12/02/2024, 1:34 PMKai Yuan
12/02/2024, 1:39 PMKlitos Kyriacou
12/02/2024, 2:49 PM@NonNull public static String foo() { return null; }
, this throw an NPE:
val x = foo()
but this works without throwing, and prints "null":
val x: String? = foo()
println(x)
I couldn't answer the OP's question given the documentation (and the Language Spec doesn't seem to say anything about this either).Robert Williams
12/02/2024, 2:56 PMRobert Williams
12/02/2024, 2:57 PMNullable/ NonNull
in Java is equivalent to explicitly stating String?/ String
in kotlinRobert Williams
12/02/2024, 2:59 PMRobert Williams
12/02/2024, 3:00 PMKlitos Kyriacou
12/02/2024, 3:05 PMfun bar1(): String? {
val x = foo()
return x
}
fun bar2(): String? {
return foo()
}
Christoph Loy
12/03/2024, 3:21 PM