Statically typed language created by Jetbrains
First commit 2010
Open Source: https://github.com/JetBrains/kotlin
(Apache License)
Pushed by oogle
as the recommended language for Android since 2019
Takes the best from other languages
Takes the best from other languages
C# - Groovy - Scala - Swift - Java
and many more
Why bother with another language ?
211000 NullPointerException on GitHub
19600 duplicates on StackOverflow
![]()
“I call it my billion-dollar mistake. It was the invention of the null reference in 1965. Which have probably caused a billion dollars of pain and damage in the last forty years.”
Null reference Java vs Kotlin
// Java
final String nullableString = null;
nullableString.lenght(); // code perfectly valid & compiled
// Kotlin
var nullableString: String? = null
nullableString.length // compiler error, not allowed
var notNullableString: String = "Null reference is forbidden"
| Java | Kotlin |
|---|---|
| Nothing enforced at type system | Nullability built into the type system |
| No check at compile time | Full check at compile time |
| NullPointerException at runtime 🤞 | No NullPointerException 🥳 |
| Optional only usable for return values | Functional constructs on nullable value |
bye bye Lombok and Apache Utils 👋
Destructuring declarations
// Java
final var firstname = person.getFirstname();
final var lastname = person.getLastname();
final var age = person.getAge();
// Kotlin
val (firstname, lastname, age) = person
Methods overloading vs named arguments
void log() {
log("Hello world", LogLevel.INFO);
}
void log(String message) {
log(message, LogLevel.INFO);
}
void log(String message, LogLevel level) {
log(message, level);
}
void log(String message, LogLevel level, Object... args) {
// since Java 14
return switch (level.name()) {
case "INFO" -> log.info(message, args);
case "ERROR" -> log.error(message, args);
default -> throw new IllegalArgumentException(
"Unrecognized level " + level.getName()
);
};
}
Methods overloading vs named arguments
fun log(message: String = "Hello world", level: LogLevel = LogLevel.INFO, vararg args: Any) {
when (level.name) {
"INFO" -> log.info(message, args)
"ERROR" -> log.error(message, args)
else -> throw IllegalArgumentException(
"Unrecognized level ${level.name}"
)
}
}
Utils vs Extension functions
// Java
final class NumberUtils {
public static boolean isEven(final int i) {
return i % 2 == 0;
}
}
NumberUtils.isEven(2); // later in the code
// Kotlin
fun Int.isEven() = this % 2 == 0
2.isEven() // later in the code
100% interoperable with Java code
// Kotlin
class AccountingService {
fun addTwoNumbers(a: Int, b: Int) = a + b
}
// Java
public class Main {
public static void main(String[] args) {
final AccountingService accountingService = new AccountingService();
int result = accountingService.addTwoNumbers(5, 7);
}
}
and vice-versa 😎
// Java
public class AccountingService {
public int addTwoNumbers(int a, int b) {
return a + b;
}
}
// Kotlin
fun main() {
val accountingService = AccountingService()
val result = accountingService.addTwoNumbers(3, 4)
}
Less boilerplate code
40% cut in the number of lines of code
Less code = less cognitive effort
Easier code to read is easier to maintain
Few hours to read - Couple of days to produce
Makes it easier to integrate a project for newcomers
Kotlin seamlessly integrates with various frameworks, offering developers flexibility and efficiency
Framework actively
supports Kotlin, allowing developers to leverage the strengths of both technologies.
Actively supported since Spring 5 (2017)
Documentation available with both Java & Kotlin examples
Adoption of the language at a large scale
Worth it or not ?
Java really well implemented at Google for many years
Kotlin introduced as Java alternative for their server-side projects 6 years ago
Result: Kotlin adoption is growing quickly
70% of devs whose primary language is Kotlin think it makes them more productive
(faster to write & language features)
87% of Google server side Kotlin devs are very satisfied with it
(easy on ramp, incremental learning & adoption)
StackOverflow survey20% less likely to crash
(Null safety, more correct code is easier to write)
0% degradation of productivity in mixed Java/Kotlin codebase
(incremental adoption, great interoperability)
(which we won't have time to cover today) 🕜
Bytecode generated at the compilation
Compilation can be slightly longer than what it is for Java
Some videos:
The git repository used for the analysis:
Some articles: