Don't use nullable types with ranges. Let's look at the following example:
fun main(args: Array<String>) {
val int = args[0].toInt()
if (int in 0..10) {
println(int)
}
}
This code, when decompiled to Java, looks as follows:
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
String var2 = args[0];
int value = Integer.parseInt(var2);
if (0 <= value) {
if (10 >= value) {
System.out.println(value);
}
}
}
As you can see, there's no overhead here. But if we use a nullable integer:
fun main(args: Array<String>) {
val value = args[0].toIntOrNull()
if (value in 0..10) {
println(value)
}
}
Then this code, when decompiled to Java, looks as follows:
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull...