If you're going to compare Kotlin and Java, why not use the principle of charity, i.e. offer the best possible case/example of your counterpart. This includes:
not adding custom toString, hash, and equals methods if you have no need for them (or just use Lombok, or at the very least either the Apache Commons or Java 8 versions of a custom equals)
Array#asList already offers a de facto immutable list, just like Kotlin's listOf, so why wrap it again in a new ArrayList<>()
why use System.out.println, when any production-worthy example would have something like LOG.info()?
as to it.length > 6, some method (or variable) extraction would be nice, since we all like self-documenting code, don't we?
for the singleton, Effective Java was published in 2001, so why are you writing a pre-2001 singleton? (i.e. the clean way to idiomatically create singletons in Java is via an enum)
Kotlin of course still wins on being more concise, even with those differences, but the difference is substantially smaller, at the cost of being substantially fairer.
Java 10 also allows you to write var list = List.of("Carlos", "Joseph", "Terry", "Katherine"); when inside a method ('var' is not allowed on members).
Arrays.asList returns an ArrayList, so why wrap?
Here is a singleton in Java:
enum Foo {
INSTANCE;
}
and if you're mutating state in a singleton, in Kotlin or Java, you're doing it wrong.
Java is far too verbose for lots of things for sure. Especially for data class type use cases. But it is much better now than it was 5 years ago. Data class like things are on the way, though they probably won't be here for 2 years.
"and if you're mutating state in a singleton, in Kotlin or Java, you're doing it wrong."
Could you make an example with a real life scenario?
I had the idea that config files or something like a ShoppingCart as a Singleton is OK.
So taking the ShoppingCart as an example, I guess it would have a List<Product> products property that allows users to add() and remove() products from it. What could be an alternative?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If you're going to compare Kotlin and Java, why not use the principle of charity, i.e. offer the best possible case/example of your counterpart. This includes:
it.length > 6
, some method (or variable) extraction would be nice, since we all like self-documenting code, don't we?Kotlin of course still wins on being more concise, even with those differences, but the difference is substantially smaller, at the cost of being substantially fairer.
Hey Alain,
Thank you for your feedback!
I'm clearly not up to date. I'll edit this post as soon as possible.
Java 10 also allows you to write
var list = List.of("Carlos", "Joseph", "Terry", "Katherine");
when inside a method ('var' is not allowed on members).Arrays.asList returns an ArrayList, so why wrap?
Here is a singleton in Java:
and if you're mutating state in a singleton, in Kotlin or Java, you're doing it wrong.
Java is far too verbose for lots of things for sure. Especially for data class type use cases. But it is much better now than it was 5 years ago. Data class like things are on the way, though they probably won't be here for 2 years.
Thanks for the tips Scott,
Regarding the Singleton:
Could you make an example with a real life scenario?
I had the idea that config files or something like a ShoppingCart as a Singleton is OK.
So taking the ShoppingCart as an example, I guess it would have a
List<Product> products
property that allows users toadd()
andremove()
products from it. What could be an alternative?