Toby Weston
Toby Weston
London, UK
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/9781484231074 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-3107-4
e-ISBN 978-1-4842-3108-1
https://doi.org/10.1007/978-1-4842-3108-1
Library of Congress Control Number: 2017963118
© Toby Weston 2018
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Printed on acid-free paper
In memory of Félix Javier García López
This book is for Java developers looking to transition to programming in Scala.
The book is split into four parts: a tour of Scala, a comparison between Java and Scala, a closer look at Scala-specific features and functional programming idioms, and finally a discussion about adopting Scala into existing Java teams.
In Part I, we’re going to take a high-level tour of Scala. You’ll get a feel for the language’s constructs and how Scala is similar in a lot of ways to Java, yet very different in others. We’ll take a look at installing Scala and using the interactive interpreter and we’ll go through some basic syntax examples.
Part II talks about key differences between Java and Scala. We’ll look at what’s missing in Scala compared to Java, vice versa, and how concepts translate from one language to another.
Then in Part III, we’ll talk about some of the language features that Scala offers that aren’t found in Java. This part also talks a little about functional programming idioms.
Finally, we’ll talk about adopting Scala into legacy Java projects and teams. It’s not always an easy transition, so we’ll look at why you would want to, and some of the challenges you might face.
Later in the book, I introduce the Scala REPL: an interactive tool for working with Scala and the Scala version of Java’s JShell. You’ll see REPL sessions prefixed with scala> .
When you do so, you can expect to be able to type in the code following scala> in the REPL verbatim, hit enter, and see the results. An example follows.
// an example REPL session
scala> val x = 6 * 9
x: Int = 54
If you don’t see the scala> prefix, assume the fragment may depend on previous code examples. I’ve tried to introduce these logically, balancing the need to show complete listings with trying to avoid pages and pages of dry code listings.
If things don’t make sense, always refer to the full source code. In short, you may find it useful to consult the full source while you read.
Infrequently, you may notice an ellipsis ( ... ) or triple question marks ( ??? ) in code fragments. When you see this, it indicates that the fragment is incomplete and will usually be followed by additional code to fill in the blanks. It probably won’t compile. It’s used when I’ve felt that additional code would be uninteresting, distracting, or when I’m building up examples.
The source code for this book is available at GitHub: https://github.com/tobyweston/learn-scala-java-devs . You can clone the repository or download an archive directly from the site.
The source code is licensed under Apache 2.0 open source license.
The book often includes partial code fragments in an attempt to avoid reams of distracting “scaffolding” code. Code may refer to previous fragments and this may not be immediately obvious. Try to read the code as if each example builds on what’s gone before.
If this style isn’t for you, I’ve also included a code listing appendix. This offers complete listings for the more complex code, in case you want to see all the code in one place. It’s not there to pad out the book. Honest.
Thanks go out to James Maggs, Alex Luker, Rhys Keepence and Xuemin Guan for their feedback on early drafts and Lee Benfield for building the excellent CFR decompiler and sharing it with the community.
Additionally, thank you to Amy Brown for providing an early copyedit of this book.
Toby Weston is an independent software developer based in London. He specializes in Java and Scala development, working in agile environments. He’s a keen blogger and writer, having written for JAXenter and authored the books Essential Acceptance Testing (Leanpub) and Learning Java Lambdas (Pact).
Jeff Friesen is a freelance teacher and software developer with an emphasis on Java. In addition to authoring Java I/O, NIO and NIO.2 (Apress) and Java Threads and the Concurrency Utilities (Apress), Jeff has written numerous articles on Java and other technologies (such as Android) for JavaWorld ( JavaWorld.com ), informIT ( informIT.com ), Java.net, SitePoint ( SitePoint.com ), and other websites. Jeff can be contacted via his website at JavaJeff.ca . or via his LinkedIn profile ( www.linkedin.com/in/javajeff ).
18.118.186.143