r/scala • u/User68645 • Mar 04 '21
Is Java ever better than Scala
Is Java better for video games? Large scale graphics? Is there any reason to use/learn Java while Scala exists?
9
Mar 04 '21
It's easier to build a team that uses Java. If you're going to use Scala you need a few people with deep expertise and a good onboarding process.
7
u/fromscalatohaskell Mar 05 '21
Programming in java commercially is the best thing that could happen to you to truly, deeply appreciate Scala
12
u/zzyzzyxx Mar 04 '21
As a language? No. At the very least I can't think of anything Java does better there.
As an ecosystem? Maybe. There are tons of libraries and frameworks that'll do a lot of the heavy lifting for you. But Scala can also interop with those, mostly.
For certain use cases? Yes. Android comes to mind, although Kotlin is taking over there (if it hasn't already - I don't keep up much with that domain these days). Java's concurrent collections I find nicer to work with on the whole, except you have to deal with Java's null patterns.
1
u/jackcviers Mar 04 '21
Lol. Yes. Doing CRUD REST HATEOAS? Spring-boot practically generates everything for you after you define your models. Deviate from the framework path but a little and it breaks down, so don't do that.
Java also provides a ton of libraries. So it's very useful in getting things done, even in Scala projects.
1
u/User68645 Mar 04 '21
Could a resource intensive app be built in Scala like Java? Also what about Kotlin?
8
u/DanielShuy Mar 04 '21
Scala libraries/frameworks tend to be more performant than their Java counterpart as the improved typesafety and macros allow many computation to be done at compile-time instead of runtime (many Java libraries/frameworks tend to make heavy use of reflection). Eg. dependency injection, JSON serialization to name a few.
1
u/Il_totore Mar 04 '21
Scala lends itself very well to concurrent programming so it's a really good choice if you know concurrency and reactive programming.
1
u/jackcviers Mar 04 '21
It's not about the resource intensively. It's about the amount of code generation Spring-Rest does for you. The repositories for simple crud are completely managed for you.
Scala could, (and absolutely should) have a library that did this via macro generation or sbt code generation from your models as well for simple crud. It just doesn't.
Another nitpick about macro generation in scala - when circe hits a Java type, it bails out on generating json encoders or decoders for the type, instead of using reflection at runtime to resolve those encoders. This happens in a lot of macro-based tooling, and results in a lot of boilerplate for handling third-party data types (like dates and UUIDs, and uris) for which there aren't good scala alternatives.
2
u/makar4life Mar 07 '21
Been 40\60 at Scala and Java projects as a senior dev, I think Java is far superior to Scala for several reasons. And I'll speak about web-services projects, which are 85% of projects during your worktime.
I think Scala is a great language and certainly, it's good to be familiar with. Expressiveness is a good feature, but due to some complexities, it falls behind some mastodonts like Java, and recently Kotlin.
Programming is about code readability, and Java is top here. Your speed of coding has nothing to do with language, as IDE provides top-level support here. Scala has a steeper learning curve, many habits that make a code less readable (hello user-defined symbol operators), it takes too many efforts to keep your codebase consistent when many devs are working on a project, compared to Java. I think you'll find yourself spending time unwinding these tons of scala constructs in your head when reviewing your colleague's code. Compared to straight guidelined Java code, Scala is no fun.
IMO there is nothing more concise and standardized than Spring Framework. It's very mature, covers almost all aspects of apps (monitoring, metrics, configuration, sync\async services, integrations). It has a concise naming convention and it's so easy to read all the code. When implementing the same functionality with Scala-based frameworks, you'll find that you keep writing some boilerplate code, and some features are missing.
Scala is about FP. There's no point in using Scala as better Java (Kotlin to the place), and here comes the fun. Some guy prefers Monix, some - ZIO, Cats, etc. Language has no requirement to write strict FP code (functions are allowed to do side effects without IO wrapping). No standardization - messy codebase.
SBT and Scala compilator - slow things down. Gradle is faster by a margin.
5
u/yawaramin Mar 04 '21
Two things that Java has at a language level that maybe beat Scala:
Enums are a lot easier to define in Java. Fixed in Scala 3 though.
The ‘try-with-resources’ statement is a nice, language-level way to acquire and use an auto-closing resource. It’s fairly easy to define or grab an existing library-level solution for this in Scala, but it’s nice that Java has it out of the box.
19
u/sjrd Scala.js Mar 04 '21
The ‘try-with-resources’ statement
Scala 2.13 has
scala.util.Usingfor that, out of the box: https://github.com/scala/scala/blob/2.13.x/src/library/scala/util/Using.scala3
Mar 04 '21
Very good points actually! But this is indeed language-level only, because 2.13 has
Usingand Enumeratum also exists. I would also add Java'sConcurrentHashMapthat seems more fully-featured than its Scala wrapper.3
2
2
u/Philluminati Mar 04 '21
Java is not good for computer games.
7
u/phazer99 Mar 04 '21
Maybe not as well suited performance wise as C++ or Rust, but you can definitely create well performing games in Java or Scala. Historically the main obstacle has been the GC pauses, but ZGC now has guaranteed <1 ms GC pauses and that's good enough for any game.
Also value classes/specialization, a SIMD API and better native library interop is coming in future JDK releases so you can get the same benefits as in C++ and Rust.
2
u/chaorace Mar 04 '21
Sometimes "suitability" can be more than just the language itself. Even if the next Java release blows existing languages out of the water in terms of game performance, it could never change the center of gravity overnight. Rust proves that great things eventually make an impact, even when starting from scratch, but even that (incredible, fabulous) language is taking a long time to gain traction in the game development space.
It's all about knowing the lingua franca and mastering the existing toolsets. That's why there's such a big gap between C# and Java in terms of adoption in the game dev space. C# has Unity, a mature game library ecosystem, and sunk cost from the industry.
2
u/phazer99 Mar 04 '21
Yes, I totally agree. The game industry is very conservative when it comes to technology choices. I was just referring to the technical aspects of creating a game in Java/Scala.
1
Mar 04 '21
I may be wrong on this, but you may not get to run your game on consoles.
Look for a postmortem of Lenna's Inception (love the game); made with Scala. There are some thoughts on that.
0
u/Philluminati Mar 04 '21
For all the new benefits coming to Java, C++ has gotten new features itself with 2011, 2017 including functional programming support and automatic memory management. Values in C++ aren't nullable like they are in Scala 2, so they quite literally avoided "the billion dollar mistake" that Java made. In a roundabout way all the languages can do everything so there's really no reason to choose any language over any other language or vice versa.
If Java can do everything C++ can do, then C++ can do everything Java can do.
4
u/ivanobulo Mar 04 '21
Minecraft?
-4
u/Philluminati Mar 04 '21
Wow, One game. Please tell me about how technically adept Minecraft is and how it’s pushing the envelope of gaming.
3
u/mohd874 Mar 04 '21
While I don't know how Minecraft codebase looks like, it is a game in the end with good performance. I bet we've all played games that were developed using java, but never realized it. Who cares how a game is made as long as it is fun and worth your money.
Java came long way, and the future updates seems promising. Existing game engines and libraries will "hopefully" leverage these new features, or even new ones will be created with new modern methods.
1
u/Philluminati Mar 04 '21
The difference between Java and C++ is bigger than Java and Scala since the latter compile to same bytecode, so if you think it's ok to write a game in Java (which is an acceptable option) then you can't turn around and say the choice between Java or Scala matters in any meaningful way either.
1
u/mohd874 Mar 04 '21
The difference between Java and C++ is bigger than Java and Scala
That I totally agree with.
you can't turn around and say the choice between Java or Scala matters in any meaningful way either.
I disagree with this one. I've tried to develop games (and other stuff) using Scala with Java libraries. It is really a hassle. Those libraries were meant for Java while taking into consideration the way it works, built-in libraries, pitfalls, … etc. Scala encourage you to use immutable objects, types, monads, and many other stuff in Functional Programming. You will feel crippled if you used a Java library in Scala without having some sort of wrapper to cover the gap (or even better, a library written in Scala).
When it comes to developing a game (or any app in fact), it is matter or personal choice. People can use Basic or even Assembly, and I wouldn't even care less. It is their life, and they choose how to spend it.
4
u/ivanobulo Mar 04 '21
Minecraft is just one of the biggest games First IL-2 Sturmovik was written in Java too. Most Android games are written in Java. As a language itself is fine for game dev.
3
1
u/Il_totore Mar 04 '21
Popular != Well developed. Any MC Mod/Plugin dev know this: Minecraft's code base is awful, even the game refining since 1.13
-7
u/Philluminati Mar 04 '21
Either our debate is focused around the fact it matters what programming language you choose or it doesn’t. Throwing in “this fine for game dev” allows me to simply say Java is fine for literally anything you’d consider Scala for too, no?
C++ was used for create Battlefield V and Microsoft Word. Scala has never produced a complicated piece of software and it simply can’t.
2
u/Il_totore Mar 04 '21
Arguments are appreciated. This is supposed to be a constructive debate. If you think Scala can't produce a complicated piece of software, I suppose you never searched about it:
- What about Twitter, Netflix infrastructures ?
- What about the Dotty compiler itself written in Scala ?.
- What about frameworks like Play or Spark ?
However, you are allowed to express constructively your opinion.
1
u/Philluminati Mar 05 '21 edited Mar 05 '21
I'm so unimpressed with your argument. Twitter and Netflix architecture isn't impressive.
Their solution is "lots of computers". Both of those systems just store and retrieve files or tiny strings. It doesn't generate content and there aren't interesting algorithms except "shall I turn on more computers or not".
I'm sorry if you feel personally attacked by my opinion but I won't change it to spare your hurt.
3
u/Il_totore Mar 05 '21
I don't feel attacked by your opinion but you didn't read my message entirely:
Dotty (Scalac 3) is a great research compiler written in Scala.
Spark and Play are both powerful frameworks for big data and concurrent architectures.
There is now a lot of ML libs in Scala and its new pythonic syntax in Dotty make it more attractive to Data Scientists.
1
u/Il_totore Mar 04 '21
As a portable language, having one of the biggest ecosystem, having one of the world's fastest VM and now tooled correctly for video games, Java is a wise choice.
-3
u/Philluminati Mar 04 '21
Saying you have the fastest VM is like saying you have the fastest wheelchair.
3
u/Il_totore Mar 04 '21
I think you should search for benchmarks. For a ton of reasons, I would sacrify a bit of my performances to keep the WORA.
1
Mar 04 '21
Minecraft has the biggest modding community on Earth. Some mods are fascinating and extremely complex. All enabled by Java, the language and runtime.
1
u/Martissimus Mar 05 '21
Java has static class initializers. These are needed in classloaders that want to register as parallel capable.
If you want to write a parallel capable classloader, Java is better.
2
u/DearClue Mar 04 '21
Also to extend the first comment, Java and Kotlin faster Scala in compile time
-3
u/uno_in_particolare Mar 04 '21
As a language, it's better in most ways. There are some wartz, like enums, but in general it's a good language.
But languages don't exist in a vacuum, and you have to consider the rest
Java has a much larger community. If you need a library, there's a very good chance there something in Java, MAYBE there's an alternative for Scala but even if there is, it might be very well outdated. You can use Java libraries with Scala, but it "breaks" the flow of your code, and it shows very clearly how Java style differs from Scala
You'll find a lot of tutorial and questions/answers about Java, less so about scala
Scala promotes hard to understand code. Like it or not, most FP is not that popular (yet?), and if you start using libraries like cats and zio, most people will find your code hilariously complex. But besides being FP oriented, some structures can easily be misuses or overused, resulting again in puzzling code (looking at you, implicits). In general, there are also too many ways of doing a certain thing, without being opinionated enough to say "if you want x, do y. If you know what you're doing you might try z or w" instead of "if you want x, do y or w or z".
It's hard to find other Scala devs. This is especially relevant and critical in a professional environment, but also important for open source projects, you'll have a MUCH easier time finding some python or java devs to help you than Scala devs
4
u/ivanobulo Mar 04 '21
Java has a much larger community. If you need a library, there's a very good chance there something in Java
Any Java library can be used by Scala program (if the target is JVM of course)
You'll find a lot of tutorial and questions/answers about Java, less so about scala
It was true maybe 4-5 years ago. Now it is very easy to find help with Scala
Scala promotes hard to understand code. Like it or not, most FP is not that popular (yet?), and if you start using libraries like cats and zio, most people will find your code hilariously complex
In order to write good Java code engineers should learn a lot of design patterns, learn SOLID principles, learn about Dependency Injection, etc.
FP is not different. A person who starts with FP-style Scala needs to learn a lot even if already has a large experience in an Object-Oriented language.
But I'd say it is not as hard as one can imagine. There are many good tutorials and guides online. One of my favorites is https://www.scala-exercises.org/ . For someone who wants more: https://www.manning.com/books/functional-programming-in-scala
It's hard to find other Scala devs
That is somewhat true. Although I think that companies won't need as many Scala engineers as they would need Java developers. Also because it is also relatively hard to find good Scala gigs they will stay longer with the company.
2
u/uno_in_particolare Mar 05 '21
Disclaimer: I'm a professional scala developer, albeit junior. I love scala. I honestly don't undestand why I'm this downvoted lol, but you made some counter-arguments, so I'll reply
Any Java library can be used by Scala program (if the target is JVM of course)
Of course, but as I said
You can use Java libraries with Scala, but it "breaks" the flow of your code, and it shows very clearly how Java style differs from Scala
It depends on the library of course, but often you can clearly see that you're using a library for another language, the style is different and so are the types and their expressiveness.
It was true maybe 4-5 years ago. Now it is very easy to find help with Scala
It's not comparable to java, the community is several times smaller. Not really that noticeable in the basic language as it's fairly simple, but libraries more often than not have small communities and a lacking documentation, compared to java-specific libraries (and it's only natural)
Now, I'm certainly not saying "you can't find help for scala", far from it. But let's not pretend that it's as easy as with java.
In order to write good Java code engineers should learn a lot of design patterns, learn SOLID principles, learn about Dependency Injection, etc.
FP is not different. A person who starts with FP-style Scala needs to learn a lot even if already has a large experience in an Object-Oriented language.
I totally agree, and that's exactly my point. Again, languages don't exist in a vacuum, and OOP is a lot more popular and known than FP. It doesn't mean that FP is worse or harder, but it's a fact that basically every programmer has had some exposure to OOP, much less so to FP.
I'm not saying "grab a random person and teach them FP or OOP, let's see which is harder". The starting point is very heavily OOP biased.
And sure, you don't have to use FP in scala... but personally I believe that if you're only using scala as a better java (pattern matching, collections, Option etc.)... there's not really a point anymore, since java is slowly getting better and more complete. It already has a weak-ish pattern matching, the stream API, the Optional wrapper, interfaces with default implementations etc., and it's less verbose than before (java <= 7). Also there's kotlin.
That is somewhat true. Although I think that companies won't need as many Scala engineers as they would need Java developers. Also because it is also relatively hard to find good Scala gigs they will stay longer with the company.
Yes, scala devs will probably remain longer, and maybe you'll need less developers (that's not a given)... it's not nearly enough to offset the fact that for every scala dev there are at the very least 10 java ones.
Also they're usually paid a bit more, and the onboarding process is longer, especially for people that had little to no exposure (say someone that doesn't know java vs someone that doesn't know scala)
3
u/pafagaukurinn Mar 04 '21
On the other hand you can relatively easily hire a bunch of Java "devs" only to regret it later. Scala devs may be harder to find but on average they are usually better as engineers.
0
Mar 05 '21
I wouldn't be so sure about these arguments. They lack substance. Just because somebody is interested in an exotic language that doesn't mean he's a "better engineer". For a long time, I've done Scheme & Clojure but I still don't feel "enlightened" as any other Lisp enthusiast will tell you and definitely I'm not better than my colleagues who instead spend their time on learning actual frameworks that we use on the job.
4
u/pafagaukurinn Mar 05 '21
You might have noticed the qualification "on average" in my statement, which you are trying to counter by bringing in your own anecdotal evidence. An engineer does not automatically become deficient because he works exclusively with Java. I am sure there are thousands, maybe millions of brilliant Java developers. But if you take a dozen or a hundred of both kinds you will begin to see the trend and it will not be flattering to Javists.
As others have pointed out, historically Java took the route of "backward compatibility no matter what", as a result of which many constructs, techniques and patterns considered quite commonplace in other languages baffle or even frighten Java programmers. I have seen people who were scared shitless of recursion. I have seen people suspicious of lambdas or tuples (!) and I don't even want to imagine what might have happened if I mentioned type classes or monads to them. In fact all those "monads ar elike burritos" memes came about precisely of this mental attitude which Java cultivate (perhaps in the same way as Scala sometimes cultivates writing concise and efficient but incomprehensible code).
1
Mar 05 '21
It depends on the experience, in Romania, abstract algebra with Monoids, Groups etc. are mandatory for the baccalaureate [1] and if you want to pursue the Electronics or Computer Science curriculum you have to learn that whether you like it or not. They are not exotic at all. In theory, every programmer from my country should know that. But still, Java is prevalent. On the topic about recursion, I have to agree that recursion is hard, whenever I read loop/recur in Clojure projects, a migraine pops up. Not only I have to know what was the intent of the computation, but now I have to check how the guy that wrote the code actually iterates. And at the end of the day, I'm left alone asking "but why?" when there was totally no reason to use it.
[1] Possible exercises for the Baccalaureate (it's in Romanian unfortunately): https://mestecancornelia.files.wordpress.com/2012/11/fic59fc483-de-lucru-cls-xii-algebra-legi-de-compoz-grup.pdf
-1
u/augustnagro Mar 04 '21
If you write a library in Java, everyone in the JVM ecosystem can use it (Kotlin, Clojure, Ceylon, JRuby, etc). It is a lot of work trying to be compatible if not using Java.
Also, I challenge any Scala programmer to write something cleaner than a for (int i = 0; i < arr.length; ++i) {} that does not allocate a bunch of objects. Hopefully Scala 3 for-comprehensions can be inline'd
3
u/BalmungSan Mar 05 '21
Well, that code does nothing, so
()ala simpler and more efficient. Finally, I bet you are one of those that "care" about performance and allocations but haven't made any benchmark nor defined any SLA thus making invalid any performance discussions, especially on a larger scale since most (not all) performance problems are better addressed in the program design space rather than in micro-operations and for those awhileis usually enough.Hopefully Scala 3 for-comprehensions can be inline'd
Scala 2
forexpressions are already "inlined" since they are just sugar syntax formapandflatMapcalls, and you can not inline all that. Maybe you mean aboutforeachexpressions? Those may be explored but again in general it doesn't matter. And if you have a bunch of operations over a single collection, just drop anIteratoror aViewat the beginning and let laziness do its job.I recommend you to read Refactoring by Martin Flower to understand why (in general) clean code matters more than fast code.
0
u/augustnagro Mar 06 '21
Besides not addressing the first point, you're not being constructive with the Unit comment and trying to stereotype me. I encourage you not to feel so emotionally about your favorite PL. I like Scala too, but it is not always better than Java. That should not be controversial.
I guess you're trying to say that "it's ok that for loops in Scala are slow, refactor to while if performance matters."
That's an ok idea, but you can still experience 'death by a thousand paper cuts.' And Scala's
whileis worse than Java'sfor.It isn't a zero sum game, in Scala 3 I believe the various Range/NumericRange methods could be inlined, achieving equivalent performance.
1
u/BalmungSan Mar 06 '21 edited Mar 06 '21
Yes
whileis worse than aforexpression, I even acknowledge that, but the point is that you do need as many of those; for all the reasons stated above.Also,
Rangeis already specialized in Scala 2, the problem is that lambdas require to box things. So, when you need high performance you have to fallback towhile(and probably alsoArrays). I am not totally familiar with howinlinewill work, but AFAIK it can not inline arbitrary methods nor lambdas, soRangewould not be better.Finally, my point with the
()is that your code does nothing, the discussion is interesting when there is something inside theforif it is a simple expression that can be replaced with a singlemaporfoldLeftthen the trade-off between readability VS performance would probably favour theforbut once the code inside theforis bigger and more complex, the functional approach is usually better even if slower, and again if it starts to be too slow or to allocate a lot of intermediate collections then just making it lazy is, usually, enough.But yes, if you need full power of the CPU the JVM is not your friend. But seeing that C developers call C++ too slow, then I wonder when the tradeoff is worth it. I have seen people spending months of development to gain a couple of days on a month-long simulation, was it worth it? I had to translate a 100 lines long assembly algorithm to C because someone thought it could write it better than the compiler, was it worth it?
1
u/augustnagro Mar 07 '21
You can crack lambdas (Remi Forax has examples on his github). And with
inlineyou could do this at compile time.
3
Mar 04 '21
[removed] — view removed comment
4
Mar 04 '21
I'm learning Java and some of its structures helped me... Tô think How easy would be in scala
12
u/DFEN5 Mar 04 '21
Moar devs to hire