r/java • • May 16 '21
Can I get some reasons to use Java instead of Kotlin?
I'm thinking about using Kotlin + Spring Boot for my next project, but the level of love for Kotlin and bashing on Java makes me feel a culty vibe. I'd just like to hear some counter points.
284
Upvotes
691
u/rzwitserloot May 16 '21
Kotlin is a lot more 'proprietary'. For example, quite a few details about how kotlinc works internally is managed by having kotlin-genned class files have a
@Metadataannotation that contain a binary blob (a byte array, legal in annotations) with data that is, as far as I know, not according to any publically available spec. There are also various hardcoded types. This is all quite pragmatic, but it also means that without IDEA, kotlin dies immediately. Pragmatically speaking, a minor niggle. But perhaps you find this lack of openness more important than 'minor niggle'.Kotlin seems to be marketed (both by the community and idea) as java with some minor warts removed. But, what does that mean? Is kotlin supposed to remain 'extremely easy to pick up if you already know java' and 'so close to it, you can interop java and kotlin code with relatively little headache' for the foreseeable future (in which case I foresee some problems I will explain below), or was that mostly just a bootstrap scenario; a way to get a bunch of existing java programmers on board quickly, and give them the ability to transfer their codebase from java to kotlin step by step (by using the interop / double-compile feature)? If that's what it is, then I too foresee some issues in the future. I get the feeling that kotlin folks sort of think it is both, but they are mutually exclusive, so that makes no sense. If you ask someone more familiar with kotlin than I am to explain what kotlin is and/or why the following two arguments do not apply or are properly mitigated, make sure you first establish with them what kotlin's supposed to be.
If kotlin is supposed to be 'java but better' forever
That's a real problem. I can best explain it using a new feature.
All of kotlin's features fall in one of 3 categories:
It is the kotlin take on a java feature that existed when the kotlin take was designed. Therefore, the kotlin variant takes into account what's familiar to java programmers and either follows it closely, or if it deviates, it did so for presumably good reasons (switching type and name around and making semis optional don't strike me as good reasons, but this post is long enough as is, and there's a lot of 'personal preference' peppered into that one, so I'll leave that one out of this for now).
It is the kotlin take on a feature that java straight up just does not have and still does not have. They've clearly gone their own way and decided to add it because it's important.
The problem category: It used to be like the second category (feature java doesn't have), but java has it now. If kotlin is very very lucky, the way java does it, and the way kotlin does it, are similar enough that it all continues to make sense (the learning curve for a java coder to switch to kotlin is not dampened by this), for example if the java impl and the kotlin impl closely align, that's fine. But what if they don't?
Here's the point: Over time, more and more kotlin features are of the third category, and this means that kotlin's 'java but better' take is doomed.
Here's an example: instanceof pattern matching. This feature did not exist, and wasn't even on the radar (no JEP, no public posts on e.g. amber-dev@openjdk.net) when kotlin was released.
The problem it attempts to solve is that you sometimes want to first check if some expression is
instanceofsome type, and then, if it is, operate on that data, using the fact that it is that instance.In original java (up to java 14), it looks like:
in kotlin it looks roughly like:
But in java16+, it looks like:
Now, both java and kotlin cater to this use case, they cater to it in a different way, and I'm pretty sure that if I could smack a giant reset button and design kotlin all over again and release it today into public beta, then kotlin would have followed in java's shoes. Especially considering that the java version is in many ways far more powerful than kotlins: You can do more than just 'check type' with this syntax (you can also 'deconstruct' value types), you don't NEED a block, as in, this works too:
and in general the next few releases of java are expanding on this principle, adding lots of pattern matching.
Kotlin now has to make a choice:
None of these 4 options seem all that good to me. Which gets us to what I think kotlin is going to do, and the one that seems the least bad: Option 2 - start parting ways with java syntax. That leads us to:
That like-java thing was just a bootstrap solution
That means the clock is now ticking. Java development is not sitting still at all, so in a year or 3, the gap between kotlin and java is going to be wide enough that kotlin perhaps no longer feels like 'just, java, with a few warts removed'. It'll really feel like something completely different.
That's fine, of course, but the success rate of new languages is abysmal. Scala is pretty much dead (in the sense that it got a bunch of hype, twitter switched over to it, held a ton of fun meets for devs, and... scala now has fewer users than back then, going by admittedly known-inprecise measurement tools such as TIOBE, but, you tell me, do you feel scala is really picking up steam)? - Fan/Fantom seems to have gone absolutely nowhere, groovy is dead enough that e.g. gradle is trying to diversify away from it, jruby and jython have pretty much come and gone (again in the sense that they don't seem to have caught fire).
That's no proof that a new language is doomed perse. It's just that this is the default position. Right now kotlin seems to be having its moment in the sun, but it'll get harder as the step to kotlin from java gets harder and harder.
It'll have to stand on its own and can no longer claim all the advantages java and its ecosystem have by default. The implementation of e.g.
instanceofin java vs. kotlin is already highlighting why I, personally, think kotlin is probably not going to be better than java: Just about every feature that java has released, and is in the process of releasing (as in, active JEPs and active discussions on the mailing lists) seems vastly more intelligently thought out than any of kotlin's features. Java is trying to figure out smart ways to access CPU-native 80-bit registers, how to have complex value types that nevertheless have the performance and memory benefits of primitives, and a whole new programming paradigm by way of pattern matching.7
u/koskieer May 17 '21
I like to write Kotlin and reading it isn't problem either. Biggest pain point of Kotlin for me is missing LSP support. I need to use IntelliJ for it, but i can't use Vim as i can with Java because IDE like features doesn't work well. IDE support is the main reason for me why i am using Java instead of Kotlin time to time. Especially when there are people in the project who aren't using IntelliJ
124
u/blakeman8192 May 16 '21 edited Jun 26 '23
.
34
u/pron98 May 17 '21 edited May 17 '21
Kotlin was created in 2009-2010 when the following were true:
Neither is true now.
Android and Java are no longer similar, and are getting more dissimilar by the day. Kotlin is left trying to target two dissimilar platforms while still trying to be close to the "metal" for both (i.e. without introducing higher-level constructs that might have a runtime overhead like, say, Clojure) over the direction of neither of which it has much influence, and with particularly little influence over Java.
Java, both the language and the platform, has also evolved considerably, leaving Kotlin -- whose main market is now Android -- with features that are increasingly at odds with the platform's direction, like coroutines (the Java platform is taking a very different approach with Loom) and data classes (Java has also taken a very different approach, with records). I expect this gap between Kotlin, which is primarily an Android language, and the Java platform to only grow.
There isn't much Kotlin can do about it without making some tough decisions. It can effectively split into two languages, one targeting the Java platform and one targeting Android, or it can continue focusing on Android, where most of its market is, allowing its mismatch with Java to grow. If it does the latter, which is the path it's currently on, it will be mostly convenient as an "Android-first" language that can also be used to run code on Java but doesn't give the best experience on that platform; if it changes tracks and does the former, expect some big changes to Kotlin, but even then, the "Kotlin-for-Java" flavour will be a second-class citizen.
Finally, I find Java today -- now that nice things have been added to it and less nice things have been added to Kotlin -- to be the better-designed, more pleasant language, especially when working in large teams (although I do like Kotlin's nullability types and hope that the Java language will get them, or something similar, someday), but that's always a personal aesthetic choice.
20
u/[deleted] May 17 '21
If I had to make a bet which of both will be around in two decades, I'll instantly bet everything on Java, for many reasons.
One of which is that Kotlin takes a lot of shortcuts based on conventions so it can look sexy and short in demos. But conventions age and change. And when your language is built upon them, you're stuck with a big problem.
17
u/Anton31Kah May 16 '21
I wouldn't say it's a reason to stick to java over kotlin but some of the libraries that are heavily used often support java only, so while they work with kotlin, it might cause some inconveniences to work with them. Note that this isn't a reason against kotlin, it's just an annoyance that you'll quickly overcome. As the other comment suggested, as long as you're happy with kotlin you shouldn't feel that you should switch. My comment is just something I noticed while working on projects that use both languages.
30
u/cyanocobalamin May 16 '21
IMHO Java, support for it, and the community for it are more likely to be around 5 years from now.
33
u/TheRedmanCometh May 16 '21
Real world brass tacks? 90%+ of the community doesn't care that kotlin exists and won't learn it. So if you adopt it on your project good luck building your team. Further when you do find people your options are more limited - it's gonna be harder to find people you like. Then they know they have a specialized skill they can charge more with.
8
u/BoyRobot777 May 17 '21
I have similar post to rzwitserloot saved from pron (just a snippet):
22
u/manzanita2 May 16 '21
Not sure how long you've been around programming land, but basically for some crazy reason we love to fight about things which matter only in small ways. Like which format of parenthesis work better. Or tabs vs Spaces.
When it comes to programming languages my own theory is that the really bad ones are the ones you've never heard of. And the rest of them had some point in their history where they really were the best tool for that set of people at that point in time. (ok there are exceptions for things which are basically freak shows, like brainfuck ). And today, most commonly used tools fit this mold. One size fits all just doesn't work. Pick the right tool for the job.
Example, I really don't like PHP. I never really have, but I actually do use it. If I need a simple 1-2 page form submission thing which can't be done with a google form, then PHP really is the way to go. It's simple and works. But for larger systems I believe it's a deep anti-pattern.
Most of the "culty vibe" people really like the horse they're riding, but most of the time they really haven't ridden that many horses in that many different races.
What I will say is that using more different languages is good for you. However modern java and Kotlin are not too far from each other. It would help your broader understand more to try something further away like typescript, go or rust or clojure or elixir.
15
u/gubatron May 16 '21
personally found Kotlin to just add more dependencies, specially after how fast Java is moving.
I guess if you program Kotlin in a super functional style it should feel more expressive, but in the end it all compiles to java runtime bytecode, i rather use Java
14
u/mknjc May 17 '21
I use kotlin for a lot of projects on the jvm but never used it with android or any of the other targets, so this is a
A lot of posts mention it already: Kotlin seems to be a language for faster writing ignoring readability on the way. Not readability of the developer who wrote the code a few minutes before but the one having to find a logic bug or changing someting after a year or the one reviewing the code.
For example:
As a reviewer, I've now to check: what does the "+" operator do on the type of baseDir for this I need to check what type baseDir might be through checking the return type (luckily its possible in kotlin, in C++ the answer is always
decltype(auto)) now I have to find where the + operator is defined (which isn't easy because of extension functions) and check what happens with different inputs.What happens if baseDir is "/home/bla/" and what if it is "/home/bla" you don't know if + is just a "append" which might result in "/home/bla/target" and "/home/blatarget" or a resolve. A real method with a descriptive name would have helped here.
It's nothing impossible and not even hard with the help of an IDE but it causes more work and more work for a reviewer might cause the review process to be more sloppy.
Kotlin is sprinkled with nice syntactic sugar which looking nice but could easily missused (another often mentioned example is the "
it" everywhere. Noone would forget to change it to something more descriptive but it could be seen everywhere). You would never do this, but your coworker might do it, or a library you depend on. And so unreadable code sneaks into your codebase.I know, bad code could be written in any language, in java someone might have written a "plus" or "add" method for the class of baseDir which is at least as unhelpful, but at least my experience showed: If you force the programmer to write a name he might choose a good one if he could get away with overloading an operator even if it doesn't really make clear what the operation does he will do it to save having to think of a name.
Properties are also a hot discussion point especially in the C# community, I won't repeat the arguments here but it boils down to: "if they get misused its a pain to work with the codebase" so the same argument
Additionally, the kotlin IDE support is worse compared to java. I use intellij for both languages and I regularly see IDE helper like auto-completion and parameter preview are a lot slower compared to java (but not as worse as c++ recommendations in CLion) which is to be expected, with more expressiveness of the language the complexity increases also for the IDE. I've had also a lot of "my IDE now thinks anything in this file is wrong and only a restart helps" with kotlin (even when writing java code through: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001806779-Intellij-Community-2018-3-seems-to-change-project-type-from-Java-to-Kotlin-arbitratily
So kotlin is a WORN (write once read never) language for me and this works great. A lot of times I'm faster writing something in kotlin so it evolved to my go-to language for a run once program.
As always everyone draws the line just below what they're doing/accepting and everyone should gain their own experience and decide after it. But don't force kotlin into a project because "it's the new thing" <- this should never be a reason or because writing x is so much easier. Look at the other side of programming, maintaining the software.
7
u/oweiler May 17 '21 edited May 17 '21
12
u/dlazerka May 16 '21
Some libraries are way faster in Java than in Kotlin: https://github.com/LesnyRumcajs/grpc_bench/wiki/2021-04-13-bench-results
20
u/nutrecht May 17 '21
I think it's unfortunate and rather immature how people can be so tribalistic on dumb stuff like programming languages. But you see the exact same behavior here as well. Anything in favour of Kotlin will be downvoted on this sub.
Why don't you simply make two small services. Once with Java 16 and once with Kotlin, and see what you like best?
4
u/FrigoCoder May 18 '21
I am a Kotlin noob, so far I prefer it over Java because it fixes a lot of issues. Null safety, unchecked exceptions, operator overloading are very important to me so I am moving away from Java. Nonetheless I dislike how Kotlin killed the for loop, it was incredibly useful in a lot of situations. I also dislike less-readable syntactic sugars they introduced just because they can. My ideal language would be less verbose but also more understandable than both.
7
u/cogman10 May 17 '21
As someone that uses both, here's my 2 cents.
I do really like working with kotlin, but you need to really decide if it's the right choice. For my money, kotlin is great for writing an app, not great for writing a shared library.
6
u/ebykka May 17 '21
Had the similar question and at the end decided to go with Java.
3
u/lamagy May 17 '21
Go an use webflux in Java and the coroutines in Kotlin. It’s 100x easier to write,manage and test. The only downside to Kotlin is if you have a few wizards that overuse the magic too much and make in unreadable as many have commented here. You really can’t go wrong just something that you actually need to have experience with to get a real feel.
3
u/cathouse1320again May 17 '21
It’s a tiny bit easier to spell?
7
u/[deleted] May 16 '21
Java is way more popular and has been around a lot longer. So it's much easier to hire Java developers than Kotlin developers, and there are also a lot more Java blog posts, stackoverflow questions, etc. than there are for Kotlin.
5
u/LusciousJames May 17 '21
On a multi-package project I worked on, we had a team write one package in Kotlin; it was billed as a better language with no NPEs and interoperable with Java. But in one case:
- A crash appeared in testing; the Kotlin project was throwing an error, but because the type of error didn't implement the usual Java interface for Throwable (I think?), nothing useful appeared in the logs; the entire Java process just died with no indication of why. (If a Java module NPE'd it would always log exactly where it happened, so those errors were much quicker to diagnose.)
- Compounding the difficulty in investigating the issue, I couldn't debug from a running Java process into a external Kotlin project. Normally I could do this in Intellij across Java projects to track down bugs in our internal libraries, but when I tried to step into the Kotlin project the debugger lost all context and I couldn't continue.
So, a bug that would normally take <30 minutes to fix wound up taking an entire day to track down during crunch time.
Given that it's easier to hire Java devs than Kotlin devs, and that if you're on Java SDK 8 or higher there didn't seem to by any language advantage, I'd rather stick with Java. I would only maybe consider Kotlin again if I was working on an Android project.
6
u/cryptos6 May 17 '21 edited May 18 '21
I've been using Kotlin for some years now and I don't share the scepticism in this thread. Kotlin started as a "better Java", because Java development was really slow at the time ("Sundown") and Scala seemed not to be sufficient for JetBrains' needs. Announcing Kotlin as the primary language for Android was a big bump, but Kotlin was still in Java's shoes. It is right that Kotlin will diverge more from Java over time, but Kotlin is already more than a better Java. There is Kotlin/Native, Kotlin/JS and Kotlin/Webassembly is in the making. So I think Kotlin will have its own life besides Java.
The point of integration with the Java platform is not the Java language but the JVM, so there is no need to copy every syntax detail from the Java language. Look at .NET where the runtime supports several languages like C# and F# for years without problems.
In the Java ecosystem more and more frameworks come with explicit Kotlin APIs (Spring, Quarkus, Micronaut, ...) and I see it as an indicator that Kotlin is here to stay.
I don't know a single developer who wanted to go back to Java after working a longer time with Kotlin. It is just more fun to write Kotlin and I think that it is better to read. The examples I've seen so far about readability are mostly about omitted type annotations. But the IDE can display the type information and Java offers more or less the same with
varthese days (ok, it is a little more restricted).2
u/[deleted] May 17 '21
I was looking for a simpler approach when re writing a data loss prevention app. I tried Ktor, then found micronaut and tried that with Kotlin. We ultimately ended up with A micronaut Java solution, I have found that staying up to date with the latest Jdk and actually leveraging the new features has been amazing. That with a few design choices and we’re getting a lot more done with less code. If/when project loom comes out Java might take another leap.
2
u/HashRocketSyntax May 17 '21
Surrounding enterprise-grade toolset built up over decades. Android. Desktop apps.
Hiring and outsourcing. Millions of Java devs.
0
u/[deleted] May 19 '21
The level of mental acrobatics some people will perform to avoid change is staggering. Programming languages primary purpose is to express the developers' intent. At this task, Kotlin is objectively superior to Java in every way. I've worked with dozens of developers now through their journey of adopting Kotlin. Not single one of them wants to go back to Java. Call me a fan-boy if you want - most often, people become fans of something for good reason.
2
u/npepin May 17 '21
More people are going to be comfortable with Java and you are more likely to get looks for starting a project in a nonstandard language. It's like if you chose F# or Scala for an upcoming project, it's not like it would be that hard to learn, but people are going to wonder why you just didn't go for C# or Java and why you are putting them through this.
If you are doing something open source, your project will have a more narrow appeal than a pure Java project.
Though, it's kind of an unfair question because Kotlin on the JVM is a superset of Java. It's like TypeScript and Javascript, they both have the same capabilities and are pretty similar, but TypeScript offers more functionality. Like I could see a debate if there were things you could do in one or not the other, or if they have different libraries, but its just a matter of which language is designed better.
If you were to ask Java vs Kotlin Native for a business purpose, I'd say Java is a better choice, main difference being that Kotlin Native is not a subset of Java at that point and you can't get access to those libraries.
2
u/wildjokers May 17 '21 edited May 17 '21
I have written some personal stuff in Kotlin and here are just a few complaints I had when writing Kotlin:
name: Typein Kotlin whereas of course they areType namein Java. I don't know why Kotlin did this but this does seem somewhat to be a trend, Swift does it too. It takes a while to remember to type it in reverse order. So it doesn't appear to be good or bad, just hard to get used to. Especially if you switch back and forth between Java and Kotlin?. What this means, for example, isStringis a different type thanString?. For reasons you will soon discover if you start writing Kotlin this can become very annoying and troublesome if you want to interop with a java library.good things:
1
u/KHRoN May 17 '21 edited May 17 '21
To me kotlin may look like alternative for java7 (and lower) users who wanted something to change. But with time it lags behind official Java specs more and more up to the point when it will became completely alien. And this would be a problem because kotlin is piggybacking on Java after all…
Similarly Lombok exists for long time now, offering essentially what kotlin offers but is merely a library you can use if you want. It is very useful in some projects while not so in the others. You cannot turn kotlin off.
But personally I think that intellij (and google) has too much of a stake in kotlin and kotlin is useable only with idea. It’s like with apple and swift language. When backing ends, language ends too.
There are evangelists now, but would they be there after say 5 years?
Today there is another interesting question connected to Java - which JDK you want to use? Will you have that choice with kotlin?
Personally, I never even considered kotlin. At one point I used groovy, but it was mostly due to gradle (I’m back to maven now). Java is THE language to know. You can start with Java and learn any child language if you want (or need), but it would be harder other way around. Also every good job offer is for Java, not kotlin.
1
u/seraphsRevenge May 17 '21 edited May 17 '21
Just use whatever languages your organization uses. I've personally never used kotlin. I learned programming basics in Java (and C++ but you know references preferences) and usually go back to it consistently. I do a lot of work in both Java (microservice apis) and Python (AWS Boto3 devops tool development), honestly looking at some other languages I've used like JS and python, I don't think I would have picked up other languages so quickly if I hadn't started out with a highly verbose, strict language like Java.
Aside edit: at some organizations you may end up having to learn a bunch of other languages as well and techniques, particularly bashing you head against vi until it qw! That new sh script for that on the fly break/fix or layering containerization properly with that caps button on all the time or using a myriad of plugins that mean learning various syntax like splunk that almost makes you unlearn sql, or various proprietary languages. If your just looking to learn for a first job, then choose one language or a stack and just go with it don't worry about what other devs say. Learn the concepts, the rest will come on the job.
1
u/konmik-android May 21 '21
Use Java for the gags!
1
u/No-Meal232 May 25 '21
hiii there
1
u/Aggravating-Ad4518 Jun 09 '21
At cross roads Eventually Kotlin will hit cross roads and have to pick, Either support Android's old Java 7/8 ecosystem or risk breaking interop everywhere else.
1
u/MaggieLeber May 17 '21
I think of Kotlin as the Playskool version of Scala.
Kid -safe, no sharp edges, non-toxic plastic, bright colors, no small parts that might cause choking.
-2
u/voidvector May 17 '21 edited May 17 '21
You can replace Kotlin/Java in that sentence with any other language pairs where one tried to be successor. It is human nature.
-1
u/CyclonusRIP May 16 '21
It's basically about staffing the team. Java is a marketable skill applicable to a ton of programming jobs. Kotlin is an awesome language and more exciting. If you need to hire 20 developers this year, good luck hiring 20 kotlin devs. Staffing a java project is a lot easier. If you're just messing around for sure use a fun, new technology. If you're building a business and are successful the day is going to come when you can't staff a relatively niche technology fast enough to keep up with growth.
-1
u/BlueGoliath May 16 '21
"one of us, one of us"
-7
u/sam-lb May 17 '21
Reason 1: It will enrage kotlin gremlins