I am totally into Scala as a language ... and still I struggle with why any company should switch from Java to Scala. Is Scala just syntatic sugar on top of the JVM or are there fundamental improvements in Scala over Java that would improve real world applications?
locked by maple_shaft♦ Oct 20 '13 at 1:04
This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.
Disclaimer: I'm not a Scala guru.
Scala does two things extremely well which Java (currently) does not.
Solve functional problems
But instead write something like:
Solve concurrency in a safer way
I can't honestly think of too much else that makes Scala stand head and shoulders above Java. Lots of small gains and improvements yes, but also far more rope to hang yourself with. YMMV
HTH a little
That depends on your definition of "just syntactic sugar". For instance, in what way is Java more than just syntactic sugar over machine code?
Any language can do less than machine code, but no language can do more.
What high level languages bring to the table is making code easier to read and understand, easier to compose, and catch more errors. And, in my opinion, it is the first of these that make most difference -- precisely "just syntactic sugar".
But considering just the other two, there are still advantages of Scala over Java.
Not to belabor the point, but having closures makes code way more composable than not having closures. And while Java 7 will add something called closures, they won't be that -- they'll just be anonymous functions.
As for catching more errors, Scala's superior handling of variance is proof enough it does so. Furthermore, its emphasis on immutability also prevent all sorts of errors -- it is not that Java can't do immutable, but it just doesn't come with the library to do so.
On top of Martijn's answer I would like to add that Scala is more expressive than Java and the benefits are that (1) it makes you more productive (2) writing less code for solving the same problem means that you can reduce the bugs in your code (IMHO bug-free code is a myth).
I've been using Scala for about 3 months now and still can't find anything I wouldn't be able to do in Java. For me literally every literature on Scala seems to mention the same thing boilerplate. If what you are looking for is reducing boilerplate then Scala is for you but IMHO, for example the filter example given above could just as well be solved using apache collections
or use closures like so
But of-course more verbose. I like the type inference though. As you learn Scala you'll realize that this is probably what is going on anyway under-the-hood. In my opinion, each language comes with +ve and -ve.
list comprehension, for comprehension.
for example in java you write:
In scala, the same code, is much more elegantly written (Like python) as:
There's so much that Scala offers that Java does not. There's simply no comparison at all. The only problem is people are more familiar with Java (b/c that's what they teach in CS classes) and not as skilled yet with the Scala paradigm.
Scala has tail recursion, it can return tuples (something that might be coming in Java 8 I think).
It's no comparison. Scala was developed by Martin Ordersky, who worked on the core team for Java Generics.
Scala is just a far superior language. That's all there is to it. People who say otherwise simply haven't explored Scala enough to know better.
Above, I meant to say tail recursion optimization (which the JVM cannot do the way that Scala's compiler can).
Scala also compiles and runs faster than JVM apps (Yes, it's true). Not to mention the frameworks. We use Tomcat for example and deploy some servelets to handle REST.
One thing Tomcat can't do, is asynchronous operations that require non-blocking I/O. For this, JAVA developers have typically invented a workaround using a message queue (send the message to the queue, and some other process or thread picks it up and does whatever you want in the background).
Unfortunately, this method is cr*p, and a hack over the limitations of deploying Java servlets on Tomcat.
Go check out akka+spray. It uses scala's Actors (Actors are just like Threads, except the only way they can communicate is through messages).
And done, asynchronous REST calls made easy. Long-running background task? No problem. Just fire and forget, and do some REST polling to check it's status from the front-end every once in a while.
If you're still using Java and think it's better than scala, you might as well stop using your computer for typing and go back to the days of quill pens and writing by candlelight. Java is basically antediluvian compared with Scala.