The Well-Grounded Java Developer:
Vital techniques of Java 7 and polyglot programming
Benjamin J. Evans and Martijn Verburg
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email:
orders@manning.com2013 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
Manning Publications Co.20 Baldwin RoadPO Box 261Shelter Island, NY 11964 |
Development editors: Renae Gregoire, Karen G. Miller Copyeditor: Andy Carroll Proofreader: Elizabeth Martin Typesetter: Dennis Dalinnik Cover designer: Marija Tudor |
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 18 17 16 15 14 13 12
Brief Table of Contents
Table of Contents
Foreword
Kirk told me I could buy beer at the petrol station, was the first sentence I heard out of Ben Evans mouth. He had come to Crete for an Open Spaces Java conference. I explained that I usually bought petrol at the petrol station, but that there was a shop around the corner that sold beer. Ben looked disappointed. I had lived on this Greek island for five years and had never thought of trying to buy beer at the local BP.
I felt a bit like this while reading this book. I consider myself a Java fundi. I have spent the past 15 years programming Java, writing hundreds of articles, speaking at conferences, and teaching advanced Java courses. And yet, when I read Ben and Martijns book, I kept coming across ideas that I hadnt thought of. They start by explaining the development effort of changing certain parts of the Java ecosystem. Changing the internals of a library is relatively easy, and we might see some improved performance for certain input. Arrays.sort() is now using TimSort, instead of MergeSort. If you sort a partially ordered array, you might see a slight performance improvement without changing your code. Changing the class file format or adding a new VM feature requires a major effort. Ben knows. He sits on the JCP Executive Committee. This book is also about Java 7, so youll learn all the new features, such as the syntactic sugar enhancements, switching on Strings, fork/join, and the Java NIO.2.
Concurrency? Thats Thread and synchronized, right? If thats all you know about multithreading, its time to upgrade your skills. As the authors point out, the area of concurrency is undergoing a massive amount of research at present. There are daily discussions on the concurrency interest mailing list, and new ideas are emerging all the time. This book shows you how to think about divide-and-conquer and how to avoid some of the safety flaws.
When I saw the chapter on classloading, I thought they had gone a bit too far. Here were the tricks that my friends and I had used to create magical code, laid bare for all to learn! They explain how javap works, a little tool that can give you insight into the bytecode generated by the Java compiler. They also cover the new invokedynamic and explain how it differs from plain reflection.
One chapter that I particularly like is Understanding performance tuning. This is the first book since Jack Shirazis Java Performance Tuning that has captured the essence of how to make your system faster. I can summarize the chapter in three words: Measure, dont guess. This is the essence of good performance tuning. Its impossible for a human to guess which code is slow. Instead of offering a single coding trick, this chapter explains performance from a hardware perspective. It also shows you how to measure the performance. An interesting little benchmark tool is their CacheTester class, which shows the cost of cache misses.
of the book explains polyglot programming on the JVM. Java is so much more than a Java programming language. Its also a platform on which other languages can run. Weve seen an explosion of different types of languages. Some are functional, some are declarative. Some are ports (Jython and JRuby), allowing other languages to run on the JVM. Languages can be dynamic (Groovy) or stable (Java and Scala). There are many reasons to use a non-Java language on the JVM. If youre starting a new project, look at whats available before deciding. You might save yourself a lot of boilerplate code.
Ben and Martijn show us three alternative languages: Groovy, Scala, and Clojure. In my opinion, these are the most viable languages at the moment. The authors describe the differences between these languages, how they compare to Java, and their special features. The chapter on each language is just enough to help you figure out which you should be using, without too much technical detail. Dont expect a reference manual to Groovy; do expect insight on which language is the right one for you.
Next, youll gain insight into how to do test-driven development and continuous integration of your system. I found it amusing that the old faithful butler Hudson was so quickly replaced with Jenkins. In any case, these are essential tools for managing your project, along with tools like Checkstyle and FindBugs.
Studying this book will help you become a well-grounded Java developer. Not only that, it will give you tips on how to stay well-grounded. Java is constantly changing. Well see lambdas and modularization in the next version. New languages are being designed; the concurrency constructs are being updated. Many of the things that you know are true now might not be true in the future. The lesson is, dont ever stop learning!
The other day I drove past the petrol station where Ben wanted to buy his beer. Like so many companies in depressed Greece, it had closed. I never did find out if they sold beer.
DR. HEINZ KABUTZTHE JAVA SPECIALISTS NEWSLETTER
Preface
This book started life as a set of training notes written for new graduate intake in the Foreign Exchange IT department of Deutsche Bank. One of us (Ben), looking at the existing books on the market, found a lack of up-to-date material aimed at inexperienced Java developers. So he resolved to write that missing book.
With the encouragement of Deutsches IT management team, Ben traveled to the Devoxx conference in Belgium to look for inspiration on additional topics. There, he met three IBM engineers (Rob Nicholson, Zoe Slattery, and Holly Cummins), who introduced him to the London Java Community (LJCLondons Java User Group).