Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Scala for Java Developers

You're reading from   Scala for Java Developers Build reactive, scalable applications and integrate Java code with the power of Scala.

Arrow left icon
Product type Paperback
Published in Apr 2014
Publisher
ISBN-13 9781783283637
Length 282 pages
Edition Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Thomas Alexandre Thomas Alexandre
Author Profile Icon Thomas Alexandre
Thomas Alexandre
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Scala for Java Developers
Credits
Foreword
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. Programming Interactively within Your Project FREE CHAPTER 2. Code Integration 3. Understanding the Scala Ecosystem 4. Testing Tools 5. Getting Started with the Play Framework 6. Database Access and the Future of ORM 7. Working with Integration and Web Services 8. Essential Properties of Modern Applications – Asynchrony and Concurrency 9. Building Reactive Web Applications 10. Scala Goodies Index

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". Raising the level of abstraction is often the terminology employed to describe programs in a more readable and natural way for humans to understand rather than the zeros and ones understood by computers.

As computer systems that are encountered in telecom, manufacturing or financial applications mature and grow, different forms of complexity tend to emerge, which are as follows:

  • Complexity due to the addition of supported features, for example, the variety of contract alternatives in an insurance system or the introduction of complicated algorithms to solve new challenges in our evolving society

  • Complexity to offset the limitations of technologies; for instance, making a system distributed to handle larger loads or improve reliability and response time

  • Accidental complexity, which is introduced because of factors other than the problem at stake, such as integration between legacy systems and not really compatible technologies, short-term workarounds to reach the consumer market in a quicker way, or misunderstanding how a large system is designed as a whole when many resources with different backgrounds and styles are contributing in a short period of time to a large code base

The third complexity is clearly unwanted and should be reduced to a minimum if not eliminated, whereas the other two should remain manageable. Scala tackles all of them, and the complexity of the business domain is something that will be manageable only if a system can be described in code as if it was described in well-written English.

In the past few years, the ability of many languages to express behaviors in a more concise way than the traditional object-oriented way is largely due to the increasing popularity of functional programming (FP), a paradigm that has been around for a very long time but until recently thought of as a competitor to the so-called imperative programming languages such as C or Java. Michael Feathers nicely outlined the apparent duality between the two in the following statement:

"OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts."

The former focuses on breaking a large system into smaller, reusable parts. These parts are easy to reason about as they are modeled according to real-life objects. They use interfaces between them and are meant to encapsulate a mutating state. The latter emphasizes on the combination of functions that have ideally no side effects. It means that their result depends only on their input arguments, leading to minimizing or removing a mutating state in a program.

The declarative nature of FP, supported by Scala, aims at writing code to express "what is to be done" rather than "how to do it". Moreover, the FP approach tends to make algorithms more concise by composing (combining functions together), whereas the imperative approach tends to introduce side effects, that is, changes in the program state that will make it more difficult to see the algorithm, in its whole, in a concise way.

This book will show Java developers that Scala is a significant yet natural evolution from Java by reasoning at a higher level of abstraction. Making the transition should ultimately lead to a more robust, maintainable, and fun software.

The intent of this book is not so much about exploring the design or deep features of the language as well as its exhaustive syntax; there are already a number of excellent books about the Scala language, notably by the creator of the language himself, Martin Odersky, and people working with him at Typesafe.

Our aim is to concentrate on helping current Java developers to get started and feel comfortable using the language, and to make their daily job more productive and fun.

What this book covers

Chapter 1, Programming Interactively within Your Project, provides a short introduction about the JVM (Java Virtual Machine) and some of the key features that have made Java successful. We will then start getting our hands dirty and experiment with the Scala REPL (short for, Read Eval Print Loop), a powerful tool to program interactively. We will introduce some of the powerful constructs of Scala that make programming not only enjoyable but also intuitive and productive.

Chapter 2, Code Integration, is about making Scala and Java code collaborate under the same code base. Topics of interest in this chapter are interoperability between Java and Scala collections, and wrapping existing Java libraries with Scala. Moreover, we will touch on the topic of coding style, in particular, by comparing the well-established Java coding best practices to the more recent Scala guidelines.

Chapter 3, Understanding the Scala Ecosystem, helps you to know the Scala development ecosystem and its surrounding tools, most of which are being more or less inherited from Java. In particular, Java frameworks such as Maven, and IDEs such as Eclipse, cannot be overlooked. In addition to the essential elements of the development cycle, we will cover Scala-specific tools such as SBT, Scala Worksheets, and the introduction of Typesafe's Activator and its templates.

Chapter 4, Testing Tools, is a follow-up on the essential tools of a Scala developer, focusing on reviewing most of the useful tools for unit, integration, and functional testing of test data as well automated property-based testing.

Chapter 5, Getting Started with the Play Framework, will give you a concrete introduction to the Play Framework, where we will show you some of the cool features of Play that make one want to migrate from a more traditional servlet/J2EE model.

Chapter 6, Database Access and the Future of ORM, covers tackling the persistence of data in relational databases, whether you want to reuse well-established technologies such as JPA/Hibernate, or move to more innovative yet promising alternatives such as SLICK (Scala Language-Integration Connection Kit), an interesting alternative to traditional ORM merely based on the power of the Scala language. Moreover, we will see how to reverse-engineer the existing relational databases into Play CRUD applications as a starting point in migrating Java projects.

Chapter 7, Working with Integration and Web Services, covers technologies that are found everywhere in today's Java development. In this chapter, we will explore how integrating with the external systems applies to the Scala world and what the benefits are. Topics included in this chapter relate to Web Services through SOAP XML, REST, and JSON.

Chapter 8, Essential Properties of Modern Applications – Asynchrony and Concurrency, refers to two aspects of scalable applications' development. To achieve better performance, software projects are often encouraged to introduce asynchronous invocations and concurrent code. Through this chapter, we will show that the more functional side of Scala can make this complexity more manageable and maintainable. We will also introduce the Akka framework, a toolkit to simplify the development of concurrent applications.

Chapter 9, Building Reactive Web Applications, takes the previous chapter one step further and introduces a new class of applications that has emerged in the market: reactive applications. They are characterized by their interactivity, the ability to push information to end users, elasticity to adapt to changes in load, and the ability to recover from failures. The aim of this chapter is to build such an app in Play using concepts learned throughout the book as well as emerging technologies such as WebSockets.

Chapter 10, Scala Goodies, concludes this book with some perspectives on the future of web development. For example, Java developers are more and more exposed to JavaScript on the client side, whether they like it or not. Another example is the emergence of Domain Specific Languages (DSLs), a nontrivial task to achieve in Java.

What you need for this book

As Scala runs on the Java Platform JVM (short for Java Virtual Machine), you will be able to write and execute the code provided in the book on any computer that supports the Java standard edition. To set up the tools that you need, refer to Chapter 1, Programming Interactively within Your Project and Chapter 3, Understanding the Scala Ecosystem

Who this book is for

This book is obviously targeted mostly for developers. We want to help Java programmers to get started and feel comfortable with both the syntax of the language and the tools. We will achieve this by 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.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

import java.util.*;
public class ListFilteringSample {
    public static void main(String[] args) {
        List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> filteredElements = new ArrayList<Integer>();
        for (Integer element : elements)
            if (element < 4) filteredElements.add(element);
        System.out.println("filteredElements:" + filteredElements);
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

import java.util.*;
public class ListFilteringSample {
    public static void main(String[] args) {
        List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> filteredElements = new ArrayList<Integer>();
        for (Integer element : elements)
            if (element < 4) filteredElements.add(element);
        System.out.println("filteredElements:" + filteredElements);
    }
}

Any command-line input or output is written as follows:

> ./activator ui 

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Out of curiosity, you may click on the Code view & Open in IDE tab and then on the Run tab."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image