days
0
2
hours
1
2
minutes
3
4
seconds
2
6
Scala Expert checklist

6 answers to the question: Why Scala and not Java?

JAX Editorial Team
Scala
Two notes stuck on message cork board image via Shutterstock

In our latest issue of JAX Magazine, we dive deeper into reactive programming and analyze the most popular JVM language and where we are heading to. After talking to Martin Odersky, the creator of Scala, about the impending 2.12 version and the current state of this programming language, we invited six Scala developers to reveal what fascinates them about Scala and why they would choose Scala over Java.

Why are you a Scala developer? What fascinates you personally about Scala?

Heiko Seeberger: What fascinates me about Scala is the possibility to write code that is not only concise but also comprehensive. The best examples for that are the case classes that depict immutable value objects with “built-in” implementations of equals and hashCode and the pattern matching which is basically “switch on steroids”. In Akka, these features are used to define how actors react to messages:



```
case class User(id: Long, name: String, email: Email)


case object GetUsers
case class AddUser(name: String, email: Email)
case class RemoveUser(id: Long)


class UserRepository extends Actor {
  ...
  override def receive = {
    case GetUsers      => // do stuff
    case AddUser(n, e) => // do stuff
    case RemoveUser(i) => // do stuff
  }
  ...
}
```

Markus Hauck: What fascinates me about Scala is the connection between functional programming on the JVM and a very expressive type system. Another bonus is the possible interoperability with Java, especially if you want to migrate from a grown Java codebase to Scala incrementally.

Daniel Westheide: Originally because I started using Java to program in a slightly more functional way but eventually got tired of the boilerplate. What fascinates me about Scala is the powerful type system. Two interesting examples: With the help of higher-kinded types and type classes it is possible to Abstract over Spark-RDDs (a Collection that is distributed over multiple systems in a cluster) and Scala collections – my application logic then knows not, if I run it in a RDD or locally on a machine on a Little Scala Collection. Such so-called Phantom types prevent me from disposing of my writing access against my Read-Slave-Database.

Ivan Kusalic:  Scala is a great language. Particularly well-suited for backend processing in an enterprise setting.

It supports a wide range of programming styles. The flavor I like most is a mix of functional and object-oriented approaches where the functional approach provides immutability and processing flow while object-oriented approach allows for good encapsulation.

Scala is an extremely expressive language and also a pleasure to work with. It allows for flexible and safe designs that are just not possible in most other languages. From simple DSLs to type classes or even macros when you really need them. I feel like I can often just directly encode my thoughts in the code without the need for cumbersome and verbose translations.

Of course – types! I can’t really imagine working in a language that doesn’t have expressive types anymore.

Finally, the beauty of how well its features build on top of one another. Having type classes possible as design pattern and not built-in future says it all.

Daniela Sfregola: In a previous life I was a Java developer. I really enjoyed the language but after a few years I realized that I wanted to try a language that is more flexible and expressive, so I decided to play a bit with Scala (at the time it was one of the new shiny languages the cool kids were playing with) and I never looked back since. In my opinion, Scala is a really fascinating and powerful language as it allows the programmer to decide how the code should look like rather than let the language syntax decide for you.

Julien Tournay: I started fiddling with Scala in late 2009. The main appeal to me at this time was lambdas which Java was cruelly lacking. At that time both Guillaume Bort, who created Play Framework, and I were working in the same company. Guillaume and the rest of the company gained interest in Scala a few month later. I jumped at the opportunity to professionally use the language, and I’ve never stopped since.

The things I liked about Scala have changed a lot as I was gaining experience.

First, lambdas were so natural to me! I was doing a lot JS development, and it may sound paradoxical, but Scala felt closer to JS than Java to me. Then as I learned more about it. I discovered type classes, pure functional programming, category theory, type level programming, logic, programming language theory, and I’m still learning !

What I love about Scala is the journey. The language helps you build better programs and can make you “grow” as a developer. You start with very down-to-earth problems (“Java is too verbose”, “I want lambdas”, “I’d like to use Spark”, etc) and as you learn the language, you’re exposed to a world of new concepts.

It’s not just libraries or tools, It’s completely new ideas. At first, it feels completely overwhelming. After a while, you just enjoy learning :)


ReactiveTo read more about reactive programming, download the latest issue of JAX Magazine:

Reactive programming means different things to different people and we are not trying to reinvent the wheel or define this concept. Instead, we are allowing our authors to prove how Scala, Lagom, Spark, Akka and Play coexist and work together to create a reactive universe.

If the definition “stream of events” does not satisfy your thirst for knowledge, get ready to find out what reactive programming means to our experts in Scala, Lagom, Spark, Akka and Play. Plus, we talked to Scala creator Martin Odersky about the impending Scala 2.12, the current state of this programming language and the technical innovations that await us.

Thirsty for more? Open the magazine and see what we have prepared for you.

 

Why Scala and not Java? In your opinion, what are the reasons to choose Scala over Java?

Heiko Seeberger: If you want to implement the above mentioned example into Java code, you would have to switch the one line case classes with multiple line classes including fields, getters, and implementations for ‘equals’ and ‘hashCode’. And instead of pattern matching, you would have to work with ‘instanceof’ and type casts. Even though some modern IDEs help you with that, the resulting code is much more verbose and ambiguous. Unlike Scala, Java is not as focused on the “What.”

Markus Hauck: Scala’s type system. Java has grave weaknesses with regard to that matter and its standardization tends to be more disturbing than helpful in practical use. It is a whole other thing with Scala: You can avoid many errors by using the Scala compiler.

Daniel Westheide: Besides the often-mentioned powerful type system, there is a whole lot of other reasons why I would usually choose Scala over Java — I would like to talk about two of them that are somehow connected to each other. First of all, with Scala you are able to define algebraic data types. The other benefit is pattern matching, which allows you to work with readable code and the aforementioned data types. The following example shows both pattern matching and algebraic data types in action. We define an algebraic data type session and discriminate between the session of a logged in user and an anonymous session. We the use pattern matching to either return a personalized or general suggestions:

sealed trait Session
case class LoggedInAs(userId: String) extends Session
case object Anonymous extends Session


						
def showRecommendations(session: Session): List[Recommendation] = session match {
  case LoggedInAs(userId) => personalizedRecommendationsFor(userId)
  case Anonymous => trendingArticles


						
}

Ivan Kusalic: Type safety, expressiveness, brevity, Functional paradigm, abstraction capabilities, etc. They are just very different languages. Scala is just so much more. Each of those topics would take hours to explore in depth on its own. Of course, there’s also a price to pay — complexity and steep learning. And hiring.

One could also argue that speed is also a reason but I find that slightly misguided as the amount of code that really needs optimization for speed as opposed to readability is very small and can even be written in Java if really needed.

But hiring — that is the only really big downside of Scala compared to Java. In basically everything else Scala wins, often by miles.

Daniela Sfregola: Although Java is evolving to embrace the advantages of a more functional style, Scala has still a lot features that Java does not have (yet) and it has a more compact syntax. This makes coding with Scala a lot more fun and productive — although, I have to admit, more difficult to learn than Java.

Julien Tournay: The real challenge in professional development to me is “scaling” a project. By this I mean to continue working on a project that becomes bigger and bigger, and working as a team with more and more developers. In my opinion, type systems help a lot to mitigate this problem.

If you are a Java developer, ask yourself this question: Why do I prefer Java to, say, Python or JS ?

If your answer is “because I like statically typed languages”, you should probably try to learn what other languages have to offer. There are languages out there with far better type systems than Java. You could try Scala of course, but also Haskell, or a ML or maybe even Idris.

Scala has the advantage of running on the JVM, so it’s easier to migrate to Scala from an existing Java code base.

Comments
comments powered by Disqus