Copyright © Cay S. Horstmann 2012. All Rights Reserved. The evolution of Java and C++ has slowed down considerably, and programmers who are eager to use more modern language features are looking elsewhere. Scala is an attractive choice; in fact, I think it is by far the most attractive choice for programmers who want to move beyond Java or C++. Scala has a concise syntax that is refreshing after the Java boilerplate. It runs on the Java virtual machine, providing access to a huge set of libraries and tools. It embraces the functional programming style without abandoning object-orientation, giving you an incremental learning path to a new paradigm. The Scala interpreter lets you run quick experiments, which makes learning Scala very enjoyable. And, last but not least, Scala is statically typed, enabling the compiler to find errors, so that you don't waste time finding them later in running programs (or worse, don't find them). I wrote this book for impatient readers who want to start programming with Scala right away. I assume you know Java, C#, or C++, and I won't bore you with explaining variables, loops, or classes. I won't exhaustively list all features of the language, I won't lecture you about the superiority of one paradigm over another, and I won't make you suffer through long and contrived examples. Instead, you will get the information that you need in compact chunks that you can read and review as needed. Scala is a big language, but you can use it effectively without knowing all of its details intimately. Martin Odersky, the creator of Scala, has identified the following levels of expertise for application programmers and library designers: Application Programmer Library Designer Overall Scala Level Beginning (A1) Intermediate (A2) Expert (A3) Junior (L1) Senior (L2) Expert (L3) Beginning Intermediate Advanced Expert
For each chapter (and occasionally for individual sections), I indicate the experience level. The chapters progress through levels A1, L1, A2, L2, A3, L3. Even if you don't want to design your own libraries, knowing about the tools that Scala provides for library designers can make you a more effective library user.
I hope you enjoy learning Scala with this book. If you find errors or have suggestions for improvement, please visit http://horstmann.com/scala and leave a comment. On that page, you will also find a link to an archive file containing all code examples from the book. I am very grateful to Dmitry Kirsanov and Alina Kirsanova who turned my manuscript from XHTML into a beautiful book, allowing me to concentrate on the content instead of fussing with the format. Every author should have it so good! Reviewers include Adrian Cumiskey (Agile Owl Software), Michael Davis (Collaborative Consulting), Daniel Sobral, Craig Tataryn, David Walend, and William Wheeler. Thanks so much for your comments and suggestions! Finally, as always, my gratitude goes to my editor, Greg Doench, for encouraging me to write this book and for his insights during the development process. Cay Horstmann
San Francisco, 2012
Topics in This Chapter
1.1 The Scala Interpreter — page 3 1.2 Declaring Values and Variables — page 5 1.3 Commonly Used Types — page 6 1.4 Arithmetic and Operator Overloading — page 7 1.5 Calling Functions and Methods — page 9 1.6 The apply Method — page 10 1.7 Scaladoc — page 10 Exercises — page 13
In this chapter, you will learn how to use Scala as an industrial-strength pocket calculator, working interactively with numbers and arithmetic operations. We introduce a number of important Scala concepts and idioms along the way. You will also learn how to browse the Scaladoc documentation at a beginner’s level. Highlights of this introduction are: • Using the Scala interpreter • Deﬁning variables with var and val • Numeric types • Using operators and functions • Navigating Scaladoc
1.1 The Scala Interpreter