small medium large xlarge

Java by Comparison: Become a Java Craftsman in 70 Examples


Cover image for Java by Comparison

Java by Comparison

Become a Java Craftsman in 70 Examples


Improve your coding skills by comparing your code to that of expert programmers so you can write code that’s clean, concise, and to the point: code that others will read with pleasure and reuse. Get hands-on advice to level up your coding style through small and understandable examples that compare flawed code to an improved solution. Discover handy tips and tricks, as well as common bugs an experienced Java programmer needs to know. Make your way from a Java novice to a master craftsman.

Customer Reviews

This is the book I’ve been waiting for since starting with intermediate Java programming
courses. Without dwelling for too long on the basics, Java by Comparison
leads you step by step to new insights on good coding practice. Instructive
examples compare the do’s and dont’s and make it easy for beginners to get the
key points. Here, the book shines and shows it’s based on long-term classroom
experience. Luckily, the book can be read as a textbook but is also useful as a
reference. I’m looking forward to using it in my classrooms. Are you sure your
style is perfect? Get it!

- Dr. Guido Wirtz

Professor for CS/Head of Distributed Systems Group, University of Bamberg

I wish Java by Comparison had been there when I was getting started with programming,
It’s like a mentor, looking over your shoulder, and patiently helping
you get your programming skills up to the next level.

- Achim Weimert

CTO, wOndary LTD

Java by Comparison is a wonderful read for all those who want to level up their
Java skills. It contains the essence of Effective Java and Clean Code, updated
with JUnit 5 and Java 8.

- Johannes Schwalb

Java Architect, uniVersa Versicherungen

The definitive guide for students to bridge the gap between the novice and the
intermediate level.

- Dr. Oliver Kopp

Postdoc, University of Stuttgart

Java by Comparison is a well-crafted quick read with good, succinct examples
updated with the latest Java 8 styling. Highly recommended for Java developers
a few years into their career.

- Ashish Bhatia

Software Engineer

I think Java by Comparison hits a sweet spot between providing useful content
and not trying to be too much. It is an easier and more directly applicable read
for junior developers than Effective Java (the best Java book). Experienced developers
will be seen nodding in agreement and likely learn a couple things along
the ride. This will be required reading for developers.

- Sebastian Larsson

Competence Team Lead, Cybercom Sweden

To become a software craftsman, you need to practice, practice, practice. Java by Comparison can accompany you during those practices. With the comparison
approach, bad code to better code, it can serve as a reference during your practice

- Zulfikar Dharmawan

Software Engineer

Java by Comparison presents a number of examples on how to improve your
coding skills and serves as a shortcut for beginners or intermediate programmers
to become experienced programmers. The book in a sense provides a lower-level
version of design patterns focusing on code structure rather than on higher-level

- Dr. Martin Blom

Associate Professor, Karlstad University

Java by Comparison is a great read if you are looking to take your Java knowledge
up another notch. I wish I could have read this book or something similar when I
first started with Java; it explains a lot of topics easily that I had to learn the hard
way. It is a must-read if you want to go from a good programmer to a great one.

- Ramaninder Singh Jhajj

Big Data Engineer, Betsson Group

Well organized and full of examples, Java by Comparison can help you whether
you are a student or a developer with years of experience.

- Emanuele Origgi

Android Software Engineer, Funambol, Inc.

With any language, once you’ve mastered its syntax, the next step is to learn how
to use it idiomatically. Whether you’re a beginner who has just started with Java
or a seasoned Java developer who started out using one of its many earlier versions,
I consider Java by Comparison a great step to become proficient in using
Java well.

- Stefan Tilkov


See All Reviews

Choose Your Format(s)

  • $21.95 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

What You Need

You need a Java 8 compiler, a text editor, and a fresh mind.
That’s it.

Contents & Extracts

  • Foreword
  • Acknowledgments
  • Welcome!
    • Who Should Read This Book
    • Why Read This Book?
    • How to Read This Book
    • Online Resources
    • Get Ready for Your Mission to Mars
  • Start Cleaning Up
    • Avoid Unnecessary Comparisons
    • Avoid Negations
    • Return Boolean Expressions Directly
    • Simplify Boolean Expressions
    • Avoid NullPointerException in Conditionals
    • Avoid Switch Fallthrough
    • Always Use Braces
    • Ensure Code Symmetry
    • What Have You Learned?
  • Level Up Your Code Style
    • Replace Magic Numbers with Constants
    • Favor Enums Over Integer Constants
    • Favor For-Each Over For Loops
    • Avoid Collection Modification During Iteration
    • Avoid Compute-Intense Operations During Iteration
    • Group with New Lines
    • Favor Format Over Concatenation
    • Favor Java API Over DIY
    • What Have You Learned?
  • Use Comments Wisely
    • Remove Superfluous Comments
    • Remove Commented-Out Code
    • Replace Comments with Constants
    • Replace Comments with Utility Methods
    • Document Implementation Decisions
    • Document Using Examples
    • Structure JavaDoc of Packages
    • Structure JavaDoc of Classes and Interfaces
    • Structure JavaDoc of Methods
    • Structure JavaDoc of Constructors
    • What Have You Learned?
  • Name Things Right
    • Use Java Naming Conventions
    • Follow Getter/Setter Conventions for Frameworks
    • Avoid Single-Letter Names
    • Avoid Abbreviations
    • Avoid Meaningless Terms
    • Use Domain Terminology
    • What Have You Learned?
  • Prepare for Things Going Wrong
    • Fail Fast
    • Always Catch Most Specific Exception
    • Explain Cause in Message
    • Avoid Breaking the Cause Chain
    • Expose Cause in Variable
    • Always Check Type Before Cast
    • Always Close Resources
    • Always Close Multiple Resources
    • Explain Empty Catch
    • What Have You Learned?
  • Assert Things Going Right
    • Structure Tests Into Given-When-Then
    • Use Meaningful Assertions
    • Expected Before Actual Value
    • Use Reasonable Tolerance Values
    • Let JUnit Handle Exceptions
    • Describe Your Tests
    • Favor Standalone Tests
    • Parametrize Your Tests
    • Cover the Edge Cases
    • What Have You Learned?
  • Design Your Objects
    • Split Method with Boolean Parameters
    • Split Method with Optional Parameters
    • Favor Abstract Over Concrete Types
    • Favor Immutable Over Mutable State
    • Combine State and Behavior
    • Avoid Leaking References
    • Avoid Returning Null
    • What Have You Learned?
  • Let Your Data Flow
    • Favor Lambdas Over Anonymous Classes
    • Favor Functional Over Imperative Style
    • Favor Method References Over Lambdas
    • Avoid Side Effects
    • Use Collect for Terminating Complex Streams
    • Avoid Exceptions in Streams
    • Favor Optional Over Null
    • Avoid Optional Fields or Parameters
    • Use Optionals as Streams
    • What Have You Learned?
  • Prepare for the Real World
    • Use Static Code Analysis Tools
    • Agree On the Java Format in Your Team
    • Automate Your Build
    • Use Continuous Integration
    • Prepare for and Deliver Into Production
    • Favor Logging Over Console Output
    • Minimize and Isolate Multithreaded Code
    • Use High-Level Concurrency Abstractions
    • Speed Up Your Program
    • Know Your Falsehoods
    • What Have You Learned?


Dr. Simon Harrer is a father, consultant, developer, and teacher who’s always looking out for new technologies, frameworks, and ideas. His friends call him a clean code evangelist, but in a good way.

Dr. Jörg Lenhard is a developer, teacher, and researcher. He has been teaching programming in Java for almost a decade and enjoys contributing to open source software. Jörg has a track record of computer science research and experience as a professional software developer.

Linus Dietz is a research assistant at the Technical University of Munich. His passion lies in developing elegant algorithms and advancing open source software.