Scala for Java Developers
Build reactive, scalable applications and integrate Java code with the power of Scala
The increasingly popular Scala programming language runs on the Java Virtual Machine (JVM). And “Java and Scala stacks can be freely mixed for totally seamless integration,” Scala’s website proudly trumpets.
The Scala site goes on to note: “Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.”
Indeed, in Scala, “every value is an object” and “every function is a value.”
Scala’s continuing rise poses something of a dilemma for many Java developers. Their questions include:
- Should I learn Scala and blend it with Java?
- Should I take up Scala and use it instead of Java?
- Should I just keep my head down, ignore Scala, and focus on getting better at developing with Java?
Thomas Alexandre’s well-written new book, Scala for Java Developers, clearly is aimed at those who would much rather add Scala to their skills and resumes than attempt to hide from the changes Scala can offer to Java development.
He notes in his preface: “When I tell people around me that I now program in Scala rather than Java, I often get the question, ‘So, in simple words, what is the main advantage of using Scala compared to Java?’ I tend to respond with this: ‘With Scala, you reason and program closer to the domain, closer to plain English.’ “
However, Alexandre’s new how-to guide does not try to browbeat developers into abandoning Java and pledging allegiance to Scala alone. Instead, Alexandre shows how the two programming languages can be used together. And he demonstrates how Scala solutions often can be shorter and less complex than their Java equivalents.
He focuses on “exploring progressively some of the new concepts brought by Scala, in particular, how to unify the best of Object-Oriented and functional programming without giving away any of the established and mature technologies built around Java for the past fifteen years.”
Decisions regarding how far to go with Scala remain with the reader. Yet Alexandre and his numerous code examples make a compelling case for becoming at least reasonably familiar with the language and experiencing how easily it can integrate with Java.
And, yes, he does evangelize a bit; this is, after all, a book about Scala. “Scala is the only language that has it all,” Alexandre touts. “It is statically typed, runs on the JVM and is totally Java compatible, is both object-oriented and functional, and is
not verbose, thereby leading to better productivity, less maintenance, and therefore more fun.”
His book is divided into 10 chapters:
- Chapter 1: Programming Interactively within Your Project – Includes advantages of using Scala, learning Scala via the REPL, and performing operations on collections.
- Chapter 2: Code Integration – Focuses on creating a REST API from an existing database, adding a test in Scala, setting up Scala within a Java Maven project, and showing how Scala and Java work together despite some differences in code style.
- Chapter 3: Understanding the Scala Ecosystem – Includes inheriting Java Integrated Development Environments (IDEs), building with Simple Build Tool (SBT), using Scala worksheets, working with HTTP, and using Typesafe Activator.
- Chapter 4: Testing Tools – Writing tests with ScalaTest and testing with ScalaCheck.
- Chapter 5: Getting Started with the Play Framework - Getting started with the classic Play distribution, getting started with the Typesafe Activator, the architecture of a Play application, authentication, and practical tips when using Play.
- Chapter 6: Database Access and the Future of ORM – In this case, ORM is Object Relational Modeling. Chapter topics include integrating an existing ORM – Hibernate and JPA, dealing with persistence in the Play framework, replacing ORM, learning about Slick, and scaffolding a Play application.
- Chapter 7: Working with Integration and Web Services – Includes binding XML data in Scala, working with XML and JSON, and handling Play requests with XML and JSON.
- Chapter 8: Essential Properties of Modern Applications – Asynchrony and Concurrency – Covers the pillars of concurrency, the async library – SIP-22-Async, and getting started with Akka.
- Chapter 9: Building Reactive Web Applications – Includes describing reactive applications, handling streams reactively, experimenting with WebSockets and Iteratees in Play, learning from activator templates, and playing with Actor Room.
Alexandre makes a strong pitch for using the Play framework in Scala web development. And he again speaks out for Scala at the conclusion of his well-structured book:
“The concise and expressive syntax of the Scala language should make your code not only more readable but also more maintainable for yourself and other developers,” he writes. “You don’t have to give up any of the libraries of the very large and mature Java ecosystem as all the APIs can be reused directly within Scala. Moreover, you benefit from many additional great Scala-specific libraries. Our recommendation is to take a piece of Java code from a domain you understand well, maybe because you wrote it in the first place one or several times before. Then, try to convert it to Scala code and refactor it to get rid of the boilerplate and to make it in a more functional style.”
– Si Dunn