This code:
if (nullableVariable != null) {
boolean success = nullableVariable.someMethodCall()
if (success) {
return success
} else {
return fallbackIfNullMethodCall()
}
} else {
return fallbackIfNullMethodCall()
}
Could also be written something like this: result = Optional.ofNullable(nullableVariable)
.map(NullableType::someMethodCall)
.orElse(fallbackIfNullMethodCall());
Sure, it's no Elvis operator, but it's a lot more readable than the "standard" if/else structure of traditional Java. If we're comparing Java and Kotlin, we should at least give Java a fair chance. I dislike the verbosity of the Optional wrapper, but it's a lot better than if/else checking.Thanks for the feedback.
result = Optional.ofNullable(nullableVariable)
.map(NullableType::someMethodCall)
.orElseGet(() -> fallbackIfNullMethodCall());This is a bad argument.
For instance, in Kotlin you can perform operations on collection without converting them to a stream first, and collecting them with a certain collector afterwards. So in order to perform a single filtering operation on a collection, you need to do three steps in Java and just one in Kotlin.
Another advantage is coroutines, which not only allow you to write asynchronous code in a common, synchronous manner, but also get rid of shared mutable state, since coroutines can send/receive shared state through channels.
>>> public static void main(final String[] args) { System.out.println("Hello world!") }
Is just a glamour shot. How many times does one actually have to type public static void main? Once per application, by definition.
Java 14 is getting an experimental preview of Records (https://openjdk.java.net/jeps/359) which takes care of much (but not all) of ceremony around "data classes".
And Java 13 got a preview of text blocks, https://openjdk.java.net/jeps/355.
Things are coming. That said, I hardly believe those minor syntax improvements are what is so "good" about Kotlin. It has some better defaults (non-null by default, final classes by default), and is a more modern language. Java will stay with us for many years to come, though.