Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share
4  
This got to have a duplicate somewhere. –  delnan May 20 '11 at 14:39
2  
It sounds like you've used it (Scala) a lot (well, more than me) - what have you found in your personal experiences? –  FrustratedWithFormsDesigner May 20 '11 at 14:39
    
I have seen questions like ... What do Java developers think of Scala, What should I do next as a Java developer, How do I kick-start my migration from Java to Scala ... but no where have I seen a question or an answer that focuses on the driving reasons for using Scala as a programming language for real world development. –  Dakotah North May 20 '11 at 14:43
1  
@delnan, at least on SO: stackoverflow.com/questions/6073517/…. @DakotahNorth, please do not cross post between SE sites - pick the forum best suited to your question and post only there. On other sites, your post will get closed down anyway, just as it happened with that one on SO. –  Péter Török May 20 '11 at 14:46
1  
Here is another, almost exact duplicate on SO, with excellent answers: stackoverflow.com/questions/2683914/… –  Péter Török May 20 '11 at 14:56
show 6 more comments

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.

5 Answers

up vote 19 down vote accepted

Disclaimer: I'm not a Scala guru.

Scala does two things extremely well which Java (currently) does not.

Solve functional problems

  • At the most basic level, Scala has fully fledged closures with collections support. This means you no longer have to write boiler plate code such as (shamelessly ripped off a DZone post)

    public List<Item> bought(User user)
    {
        List<Item> result = new ArrayList();
        for (Item item : currentItems)
        {
            if (user.bought(item))
            {
                result.add(item);
            }
        }
        return result;
    }
    

But instead write something like:

def bought(user: User) = items.filter(user bought _)
  • There's more functional love, but I'm not qualified to talk about it since I currently still suck at functional programming :)

Solve concurrency in a safer way

  • Scala has an actors model (+ some other goodness) which is inheritly safer than Java's mutable data + locks on Thread model (no matter how good the libs are getting, Java is still hindered by the language).

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

share
3  
I would like to point out that akka (actor model) is available for both Scala and Java. See akka.io –  Giorgio May 26 '12 at 7:06
5  
I like Scala and I am migrating to it from Java. Still it pisses me off when Java and Scala are compared and Scala developers try to write as verbose and multi line Java code as possible and try very hard to replace it with Scala one liner. Without loss of readability above Java code could fit in 5 lines , not 12 –  lucek Apr 10 '13 at 19:31
2  
"Lots of small gains and improvements yes, but also far more rope to hang yourself with." +1 –  robjb Oct 16 '13 at 19:28
    
@lucek Especially since the snippet uses C's curly braces convention instead of Java's :P –  Andres F. Oct 16 '13 at 20:31
    
@robjb: "Lots of small gains and improvements yes, but also far more rope to hang yourself with." I do not agree that Scala gives you more rope to hang yourself with. At least, there is no explanation for this statement in the answer and I cannot see one myself. Also, Scala does not just introduce a few functional idioms on top of an OO language (like e.g. C# and Java 8), it tries to integrate OOP and FP into one paradigm. IMHO this is not "small improvements" but rather a paradigm switch. –  Giorgio Oct 18 '13 at 5:27
add comment

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.

share
4  
Actually Java's 'closures' aren't coming until Java 8 –  Martijn Verburg May 21 '11 at 8:30
1  
@Martijn Thanks for the correction. At this point, I don't actually care anymore. –  Daniel C. Sobral May 22 '11 at 2:07
1  
I would say that syntactic sugar is just alternative syntax on top of existing semantics. Since the semantics of machine code does not include objects, classes, etc, I think Java is not just syntactic sugar over machine code. The semantics and programming paradigm is different. –  Giorgio May 26 '12 at 7:10
    
@Martijn Verburg: Java has a form of closures already (in the form of anonymous inner classes). What it lacks are anonymous functions (which can be thought of as special anonymous classes with exactly one method and some special syntax). –  Giorgio May 26 '12 at 7:12
    
@Giorgio - true, but the anon inner class is poorly performing compared to the upcoming invokedynamic based implementation and it's well, source code ugly IMO :-) –  Martijn Verburg May 26 '12 at 9:36
show 5 more comments

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).

share
add comment

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

<T> CollectionUtils.filter(Predicate<T>...)

or use closures like so

<T> CollectionUtils.forAllDo(..., Closure<T>)

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.

share
add comment

list comprehension, for comprehension.

for example in java you write:

int myVar;
if (condition) {
  myVar = //some value
}
else {
 myVar = //some other value
}

In scala, the same code, is much more elegantly written (Like python) as:

int myVar = (//some value) if (condition) else // other value;

and done.

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.

share
4  
This is not an example of a list comprehension. And in Java you write: int myVar = condition ? someValue : otherValue –  kevin cline Oct 16 '13 at 19:14
1  
You should edit those //some value //other value comments to /*some value*/ style ones or something. It's currently messing up your syntax highlighting :p –  KChaloux Oct 16 '13 at 20:49
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.