Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering High Performance with Kotlin

You're reading from   Mastering High Performance with Kotlin Overcome performance difficulties in Kotlin with a range of exciting techniques and solutions

Arrow left icon
Product type Paperback
Published in Jun 2018
Publisher Packt
ISBN-13 9781788996648
Length 316 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Igor Kucherenko Igor Kucherenko
Author Profile Icon Igor Kucherenko
Igor Kucherenko
Arrow right icon
View More author details
Toc

Ranges

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime