Rough Cuts are manuscripts that are developed but not yet published, available through Safari. Rough Cuts provide you access to the very latest information on a given topic and offer you the opportunity to interact with the author to influence the final publication.
This is the Rough Cut version of the printed book.
Scala is a modern programming language for the Java Virtual Machine (JVM) that combines the best features of object-oriented and functional programming languages. Using Scala, you can write programs more concisely than in Java, as well as leverage the full power of concurrency. Since Scala runs on the JVM, it can access any Java library and is interoperable with Java frameworks.
Scala for the Impatient concisely shows developers what Scala can do and how to do it. In this book, Cay Horstmann, the principal author of the international best-selling Core Java™, offers a rapid, code-based introduction that’s completely practical. Horstmann introduces Scala concepts and techniques in “blog-sized” chunks that you can quickly master and apply. Hands-on activities guide you through well-defined stages of competency, from basic to expert. Coverage includes
Scala is rapidly reaching a tipping point that will reshape the experience of programming. This book will help object-oriented programmers build on their existing skills, allowing them to immediately construct useful applications as they gradually master advanced programming techniques.
Foreword xvii
Preface xix
About the Author xxi
Chapter 1: The Basics 1
1.1 The Scala Interpreter 1
1.2 Declaring Values and Variables 3
1.3 Commonly Used Types 4
1.4 Arithmetic and Operator Overloading 5
1.5 Calling Functions and Methods 7
1.6 The apply Method 8
1.7 Scaladoc 8
Exercises 11
Chapter 2: Control Structures and Functions 13
2.1 Conditional Expressions 14
2.2 Statement Termination 15
2.3 Block Expressions and Assignments 16
2.4 Input and Output 17
2.5 Loops 18
2.6 Advanced for Loops and for Comprehensions 19
2.7 Functions 20
2.8 Default and Named Arguments 21
2.9 Variable Arguments 22
2.10 Procedures 23
2.11 Lazy Values 23
2.12 Exceptions 24
Exercises 26
Chapter 3: Working with Arrays 29
3.1 Fixed-Length Arrays 29
3.2 Variable-Length Arrays: Array Buffers 30
3.3 Traversing Arrays and Array Buffers 31
3.4 Transforming Arrays 32
3.5 Common Algorithms 34
3.6 Deciphering Scaladoc 35
3.7 Multidimensional Arrays 37
3.8 Interoperating with Java 37
Exercises 38
Chapter 4: Maps and Tuples 41
4.1 Constructing a Map 41
4.2 Accessing Map Values 42
4.3 Updating Map Values 43
4.4 Iterating over Maps 43
4.5 Sorted Maps 44
4.6 Interoperating with Java 44
4.7 Tuples 45
4.8 Zipping 46
Exercises 46
Chapter 5: Classes 49
5.1 Simple Classes and Parameterless Methods 49
5.2 Properties with Getters and Setters 50
5.3 Properties with Only Getters 53
5.4 Object-Private Fields 54
5.5 Bean Properties 55
5.6 Auxiliary Constructors 56
5.7 The Primary Constructor 57
5.8 Nested Classes 60
Exercises 63
Chapter 6: Objects 65
6.1 Singletons 65
6.2 Companion Objects 66
6.3 Objects Extending a Class or Trait 67
6.4 The apply Method 67
6.5 Application Objects 68
6.6 Enumerations 69
Exercises 71
Chapter 7: Packages and Imports 73
7.1 Packages 74
7.2 Scope Rules 75
7.3 Chained Package Clauses 77