Tell me more ×
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|improve this question
3  
This got to have a duplicate somewhere. – delnan May 20 '11 at 14:39
1  
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 4 more comments

3 Answers

up vote 14 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|improve this answer
1  
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
2  
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 at 19:31

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|improve this answer
3  
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
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 3 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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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