Diving into Scala: A Conversation with Java Champion Cay Horstmann

by Janice J. Heiss

Learn what Scala has to offer the Java developer.

Published June 2013

Scala, a statically typed object-functional programming and scripting language for the Java Virtual Machine (JVM), has gotten some rave reviews of late. Noted Java Champion Dick Wall remarked that Scala "has about every element I would include in a language." Wall, a Scala trainer, has written that once the initial learning barrier is crossed, "Developers in Scala typically find themselves several times more productive than in Java."

More recently, Java Champion Cay Horstmann has thrown his hat into the ring with his much lauded book, Scala for the Impatient, which has a foreward written by none other than Martin Odersky, the inventor of Scala who called it “a joy to read” and the “best introduction to Scala”. Odersky liked the basic concept of the book so much that he asked Horstmann if the first section could be made available as a free download on the Typesafe Website, something Horstmann graciously assented to.

Horstmann, a professor of computer science at San Jose State University and a frequent speaker at computer-industry conferences, has written many books on C++, Java, and object-oriented development, and is perhaps best known for his Core Java series, coauthored with Gary Cornell. Earlier in his career, he spent four years as VP and CTO of an internet startup that grew from three people in a tiny office to a public company. He holds a PhD in mathematics from the University of Michigan.

We met with him to get his take on Scala.

Cay Horstmann
Cay Horstmann

Oracle Technology Network: Let's say you are an experienced programmer seeking greater expressiveness. What does Scala offer you?

Horstmann: There are several levels at which you can benefit from Scala. For example, you can write classes with less boilerplate, file and XML handling is more concise, and you can replace tedious loops over collections with more elegant constructs. Typically, programmers at this level report that they write about half the number of lines of code in Scala that they would in Java, and that's nothing to sneeze at. Another entry point can be if you want to use a Scala-based framework such as Akka or Play; you can use these with Java, but the Scala API is more enjoyable. 

“You might have an idea for the next great framework in your problem domain. Sure, you could author it in Java, but Scala gives you many more tools to make your ramework developer-friendly. ”

Cay Horstmann
Java Champion

Alternatively, you might have an idea for the next great framework in your problem domain. Sure, you could author it in Java, but Scala gives you many more tools to make your framework developer-friendly.

There is another use case that is dear to my heart. I often have to do some exploratory programming, slicing and dicing a data set, or transcoding XML from one format to another. With the Scala REPL (read/evaluate/print loop), I can blaze a trail through the messy world of real-life data and quickly come up with a solution.

Oracle Technology Network: One Web reviewer, K. Ferrio, said that Scala for the Impatient breaks new ground because it does not jump immediately into functional programming; it establishes common ground with Java and C++ programmers; and it dispatches re-mappable differences between Java and Scala before introducing new concepts. What guided you in these strategies?

Horstmann: I did quite a bit of functional programming as a graduate student "way back when." I still love Lisp and Scheme, but when I had to solve real problems after I graduated, I didn't often use those languages, for many pragmatic reasons. Let's face it, object-oriented programming is pretty effective most of the time. So, when I saw some Scala books tell their readers that they had sinned and that they should repent, I felt that was a bit presumptuous. Why not tell the readers that Scala can help them with object-oriented programming, and also give them another set of tools that are based on functional programming?

Scala and Theory

Oracle Technology Network: Your book spends very little time on the theory behind Scala. What is the most important thing for developers to understand about Scala theory?

Horstmann: Some people learn best by first being exposed to the theory, but for most people, myself included, examples work better. Of course, when you learn something in college, or in a technical book, it is common to first see the theory and then, maybe, if you're really lucky, an application. Did your eyes just glaze over? That's because I didn't give an example.

Here is one. When I explain the map function, I don't say that it transforms a collection into another by applying a function to each element. Instead, I show this:

val names = List("Peter", "Paul", "Mary")
names.map(_.toUpperCase) // List("PETER", "PAUL", "MARY")

Now you can see with your own eyes that map transforms a list, and you can probably guess that _.foo means "the function that maps x to x.foo." And what did I just do? I gave you an example of how to explain stuff, instead of dwelling on the theory.

Let me give you another example. I am learning French right now—which, by the way, is a lot more complex than Scala. So, my grammar book says, "It is impossible to use "dont" (the equivalent of "which" in French) after composite prepositions. I am completely baffled by this seemingly arbitrary rule, but fortunately, the book immediately gives an example, because the author has no confidence that the reader will grasp it otherwise.

Theory is great—but not for learning something new. We carbon life forms are wired to learn by experience. Not by theory. After we have learned something, we can use the theory to make it easier to learn the next thing. That's what makes us human.

Oracle Technology Network: Can you be a good Scala developer without a grasp of the deeper concepts and theories that underlie it? 

“Scala has been very well designed so that you can use terrifically useful features without having to worry about theory. ”

Cay Horstmann
Java Champion

Horstmann: Sure you can. The vast majority of Java developers have only the sketchiest grasp of overloading resolution or how wildcards work in generics, but that doesn't stop them from being useful. I always find it comical, when talking to Rails developers, how little they know about the Ruby metaobject protocol that makes Rails possible, yet they are perfectly productive. I'd argue that a language that requires all users to be versed in deep theories is mis-designed. It is the job of the language designer to be aware of those deeper concepts and shape the language so that it is natural for its programmers to do the right thing without being theorists themselves. Fortunately, Scala has been very well designed so that you can use terrifically useful features without having to worry about theory.

Oracle Technology Network: What do people like most about your book?

Horstmann: Much to my surprise, the exercises. When I wrote Core Java (the first serious book about Java way back when, in 1995), I was told sternly by my editor "no section numbers and no exercises, or people will think it's (gasp) a college book." But in the Scala book, I said, "what the heck, I'll give exercises another shot." And I got so many positive responses that readers really liked trying out their newfound knowledge.

Misconceptions About Scala

Oracle Technology Network: Many developers approach Scala with trepidation. What are the biggest misconceptions that you encounter about Scala?

Horstmann: I think many developers are scared because someone tells them that some dark corner of the language is really complex. And you know what? Some dark corners of the language really are complex. Personally, I just stay away from those corners. Do you want to know what language is really simple and without a single dark corner? That would be Scheme.

So, in real life, you can choose between the pristine elegance and simplicity of Scheme, or, maybe, Java 1.0 (without inner classes or generics), or the potentially mind-boggling complexity of Java SE 8, C# 4.0, C++ 11, Apple's Objective-C implementation, or, well, Scala.

Oracle Technology Network: You write that Scala is the most attractive choice for developers who want to move beyond Java and C++. Why? What are the other choices?

Horstmann: Clojure is pretty nice, but I found its Lisp syntax a bit off-putting, and it seems very focused on software transactional memory, which isn't all that useful to me. And it's not statically typed. I wanted to like Groovy, but it really bothers me that the semantics seems under-defined and in flux. And it's not statically typed. Yes, there is Groovy++, but that's in even sketchier shape.

There are a couple of contenders such as Kotlin and Ceylon, but so far they aren't real.

So, if you want to do work with a statically typed language on the JVM that exists today, Scala is simply the pragmatic choice. It's a good thing that it's such a nice choice.

Pushing the JVM

Oracle Technology Network: Java Champion Dick Wall remarked that Scala pushes the JVM harder than other JVM languages. "Scala tries to be more than Java in every way," said Wall. "It is more object-oriented, more functional, more statically typed, more flexible, and more concise, and in many other ways, it pushes the boundaries of what is possible." Your comments?

Horstmann: Well, Dick is right. Fortunately, as a user of the language, you don't worry too much about the fact that the implementers do that pushing. You just take advantage of that. One might well argue that the JVM, designed twenty years ago, shows its age, and that maybe a new VM would be better. But a highly performing VM isn't designed every day, and the Scala developers are doing the right thing by using the JVM.

Oracle Technology Network: Is Scala more complicated than Java?

Horstmann: It's both simpler and more complicated. In Scala, you don't worry much about int versus Integer, or arrays versus ArrayList, or single-abstract-method interfaces versus functions. But Scala has many complex features. I agree with Brian Goetz that if the Scala reference had the level of detail of the Java language specification, it would be quite a bit longer.

Oracle Technology Network: Again, Dick Wall: "Developers in Scala typically find themselves several times more productive than in Java on large projects once the initial learning barrier is crossed." Does this jibe with your experience?

Horstmann: Sure, and that comes from two areas. First off, it is just liberating to be rid of the boring boilerplate. That's the 2x improvement. Then, because you have tools available that you simply don't have in Java, you can build your own structures that further reduce your own drudgery, as well as that of the programmers to whom you provide services.

Oracle Technology Network: Are certain kinds of Java developers better suited to learn Scala than others? What skill set makes a developer best suited to learn Scala? 

“if you are wondering why there is all that boilerplate in Java and what you could do to be more productive, why not check out Scala? ”

Cay Horstmann
Java Champion

Horstmann: I suppose there are some developers who just point and click and then go home, and they wouldn't benefit much from Scala. But in general, I have found Java programmers to be an inquisitive bunch who want to know what the next big thing is and what it can do for them. So, if you are wondering why there is all that boilerplate in Java and what you could do to be more productive, why not check out Scala? Even if you don't use it everyday, the idioms that you pick up will make you a more productive Java developer.

Oracle Technology Network: What role does Scala play in your own life as a developer? How do you make use of it?

Horstmann: Like I said, I use Scala for very tactical purposes: to transcode data and XML formats. And I am working on a very nifty framework for producing graphs and diagrams, something that I often need to do for my books.

Oracle Technology Network: A common complaint about Scala is the weakness of IDE support. How much has this improved of late?

Horstmann: The question is, "Compared to what?" Java has exceptionally wonderful IDE support, and Scala is still catching up. But I was recently working with Python and Objective-C, and I found their IDE support much weaker than IDE support for Scala. (Yes, I am looking at you, XCode. Just saying it's magical doesn't make it so.)

An Update on Scala 2.10

Oracle Technology Network: The final version of Scala 2.10 was released on January 4, 2013. You have been toying around with it a bit. Give us an update on some of the new features not covered in your book.

Horstmann: I've been fussing with dynamic invocation that lets you add capabilities in a Ruby-like flavor, and I had a graduate student who exercised the macro features, which still seem like a work in progress. It takes a lot of work to implement interesting features with macros. I do like string interpolation a lot—no more print statements that look like "label: " + value + "another label: " + anotherValue. Anyway, it's an incremental update.

One thing that I like about Scala is the willingness to experiment with new features, put them out there in front of developers, and every so often say "maybe that didn't work so well," and fix it. In Java, no idea, no matter how poorly conceived, ever dies, because backwards compatibility is the core value. The Scala model seems more compatible with the way that we, the carbon life forms that crunch out software, actually work.

See Also

About the Author

Janice J. Heiss is the Java acquisitions editor at Oracle and a technology editor at Java Magazine.

Join the Conversation

Join the Java community conversation on Facebook, Twitter, and the Oracle Java Blog!