Java Reference
In-Depth Information
Chapter 15. Blending OOP and FP: comparing Java 8
and Scala
This chapter covers
An introduction to Scala
How Java 8 relates to Scala and vice versa
How functions in Scala compare to Java 8
Classes and traits
Scala is a programming language that mixes object-oriented and functional programming. It's
often seen as an alternative language to Java for programmers who want functional features in a
statically typed programming language that runs on the JVM while keeping a Java feel. Scala
introduces many more features compared to Java: a more sophisticated type system, type
inference, pattern matching (as presented in section 14.4 ) , constructs to simply define domain
specific languages, and so on. In addition, you can access all Java libraries within Scala code.
You may be wondering why we have a chapter about Scala in a Java 8 book. This topic has been
largely centered on adopting functional-style programming in Java. Scala, just like Java 8,
supports the concepts of functional-style processing of collections (that is, stream-like
operations), first-class functions, and default methods. But Scala pushes these ideas further: it
provides a larger set of features around these ideas compared to Java 8. We believe you may find
it interesting to compare Scala with the approach taken by Java 8 and see Java 8's limitations.
This chapter aims to shed light on this matter to appease your curiosity.
Keep in mind that the purpose of this chapter is not to teach you how to write idiomatic Scala
code or everything about Scala. There are many features such as pattern matching, for
comprehensions and implicits supported in Scala but not in Java, that we won't discuss. Rather,
this chapter focuses on comparing the new Java 8 features to what Scala provides, so you have
an idea of the bigger picture. For example, you'll find that you can write more concise and
readable code in Scala compared to Java.
This chapter starts with an introduction to Scala: how to write simple programs and working
with collections. Next, we discuss functions in Scala: first-class functions, closures, and currying.
Finally, we look at classes in Scala and a feature called traits : Scala's take on interfaces and
default methods.
 
Search WWH ::




Custom Search