Become a Better Java Developer

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 decent developer.

Learn more
Cover of the book Java by Comparison

Become a Better Java Developer in 70 Examples

A Mentor for you

This book is a useful companion for anyone learning to write clean Java code. The authors introduce you to the fundamentals of becoming a software developer, by comparing pieces of problematic code with an improved version, to help you to develop a sense for clean code. This unique before-and-after approach teaches you to create clean Java code.

Code Cleanly

Keep your booleans in check, get rid of magic numbers, and use the right style of iteration. Comment wisely, name things accurately, handle exceptions robustly, test with JUnit 5, and write elegant functional and object-oriented code.

Become a Developer

Writing excellent code isn’t just about implementing the functionality. It’s about the small important details that make your code more readable, maintainable, flexible, robust, and faster. Java by Comparison teaches you to spot these details and trains you to become a better programmer.

  • 70 Examples
  • 206 Pages
  • 15 Reviewers
  • uncountable Cups of Coffee

Who Is This Book For?

For anyone learning to program in Java at a beginner or intermediate level, and for teachers and senior developers who coach new developers.

Before reading

  • You know the Java basics: conditions, loops, classes and objects, simple algorithms, and lists, queues, stacks and maps.
  • You can solve FizzBuzz in Java. That is the readiness check from the preface.
  • Your code works, but you are not sure it is clean, readable, and easy to maintain.

After reading

  • You spot common code smells in booleans, formatting, magic numbers, and iteration.
  • You name things right, comment wisely, and handle exceptions robustly.
  • You test with JUnit 5 and use a functional style and clear object-oriented design.
  • You have internalized a checklist of 70 items to review your own and others' code.

Already know Effective Java and Clean Code? You will still find something new. All you need is a Java 8 compiler, a text editor, and a fresh mind. For newer Java, up to Java 25, see the free Modern Java and Java Concurrency comparisons.

What's in the book...

Welcome! (free sample)

Welcome to Java by Comparison! Get ready for code quality.

Start Cleaning Up

Learn the basics of code understandability and start cleaning your code.

Return Boolean Expressions Directly (free sample)

Level Up Your Code Style

Get the quality of your Java code to the next stage.

Use Comments Wisely

Write meaningful comments when they're needed and avoid them when not.

Document Using Examples (free sample)

Name Things Right

Find concise names for your code structures that comply to the conventions.

Prepare for Things Going Wrong

Make your program robust with clean exception handling

Assert Things Going Right

Use JUnit5 to test your code.

Design Your Objects

Apply object-oriented design principles to make your program flexible and maintainable.

Split Method with Boolean Parameters (free sample)

Let Your Data Flow

Leverage the power of lambda expressions and functional programming in Java.

Prepare for the Real World

Use static analyis, build automation and concurrency to release your program into the wild.

Browse all 70 comparisons of the book

The anatomy of a comparison: actionable name, bad code with problem description, better code with solution description
Every item has the same anatomy: an actionable name, the bad code with a problem description, and the better code with a solution description.

Comparisons Online

Besides the table of contents of the book, we publish further before/after comparisons on the web, with code and explanations.

What Happy Readers Say

Profile of Achim Weimert

Achim Weimert

CTO

quote sign left 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. quote sign right

Profile of Dr. Guido Wirtz

Dr. Guido Wirtz

Professor for Computer Science

quote sign left 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! quote sign right

Profile of Ashish Bhatia

Ashish Bhatia

Software Engineer

quote sign left 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. quote sign right

Profile of Stefan Tilkov

Stefan Tilkov

CEO

quote sign left 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. quote sign right

Profile of Johannes Schwalb

Johannes Schwalb

Java Architect

quote sign left 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. quote sign right

Get the Book

Used in University Courses

The book grew out of more than six years of teaching. Today it is course literature at universities in Germany, the Netherlands, and Sweden, and the kata runs in company coding dojos.

  • University of Bamberg logo

    University of Bamberg

    Advanced Java Programming · Parallel and Distributed Programming
  • Hochschule München

    Softwareentwicklung I · software development lectures
  • Vrije Universiteit Amsterdam logo

    Vrije Universiteit Amsterdam

    Software Design
  • Stockholm University logo

    Stockholm University

    Programmering 1 · testing of IT systems · course package

Teaching with the book

Do you teach with Java by Comparison? Tell us about your course.

The Science Behind the Book: Teaching Clean Code

The before/after method was developed and evaluated in university teaching and published in peer-reviewed software engineering education workshops. Read the story of the book and the research behind it.

  • Teaching Clean Code

    Linus W. Dietz, Johannes Manner, Simon Harrer, Jörg Lenhard. ISEE 2018 at SE 2018, Ulm. CEUR Workshop Proceedings Vol. 2066, pp. 24–27.

    Describes a feedback-driven course for 2nd and 3rd year students that puts clean code ahead of mere correctness, refined over more than six years and awarded a faculty teaching award. It proposes a knowledge base of before/after examples, the idea behind this book.

  • Code Process Metrics in University Programming Education

    Linus W. Dietz, Robin Lichtenthäler, Adam Tornhill, Simon Harrer. ISEE 2019 at SE 2019, Stuttgart. CEUR Workshop Proceedings Vol. 2308, pp. 23–26.

    Mines the Git repositories of 38 student teams in two Bamberg courses that use Java by Comparison in their code reviews, and analyzes how code process metrics relate to students' grades.

About the three Authors

Take a closer look into our amazing team. We won’t bite.

Profile Image of Dr. Simon Harrer

Dr. Simon Harrer

Co-Founder & CEO, Entropy Data

Simon is a product engineer at heart who co-founded Entropy Data, a data product marketplace for humans and agents. He taught Java at the University of Bamberg for years, and his friends call him a clean code evangelist, but in a good way. simonharrer.com

Profile Image of Dr. Jörg Lenhard

Dr. Jörg Lenhard

Developer

Jörg is a developer, teacher, and researcher. He has been teaching programming in Java for almost a decade and enjoys contributing to open source software.

Profile Image of Linus Dietz

Dr. Linus Dietz

Lecturer, King's College London

Linus is Lecturer (Assistant Professor) in Computer Science at King's College London. He researches data science and recommender systems and works on computer science education. linusdietz.com

Press Coverage

Reviewed in magazines, on developer portals, blogs, and podcasts, in five languages.

  • Java Magazine

    Review by Andrew Binstock, editor in chief.

    Read the review
  • InfoQ

    Review by Jeanne Boyarsky, also translated into Japanese.

    Read the review
  • heise Developer

    Review by Michael Müller (German).

    Read the review
  • DZone

    Reviews by Dustin Marx and Ken Fogel.

    Read the review
  • Robert C. Martin

    Short review by "Uncle Bob", author of Clean Code.

    Read the post
  • airhacks.fm

    Adam Bien interviews Simon Harrer on his podcast.

    Listen
Show all 47 reviews and interviews

Links to pages that no longer exist point to their copy in the Internet Archive.

Fun fact: Java by Comparison is one of the books in the Bartz v. Anthropic copyright settlement, the class action over books used to train Anthropic's Claude models. In a way, its comparisons live on in Anthropic's frontier models.

Awards

Translations and Libraries

  • Korean edition

    자바 코딩의 기술, published by Gilbut in 2020, translated by 심지현.

    All editions and translations
  • In university libraries

    HTW Berlin, HU Berlin, TH Hamburg, TH Köln, TH Nürnberg, TU Darmstadt, TU München, Uni Bamberg, Uni Bayreuth, Uni Dresden, Uni Erlangen, Uni Hamburg, Uni Passau, Uni Stuttgart, Uni Weimar, Uni Würzburg, and more.

Talks and Events

Upcoming

No talks are scheduled right now. Want a talk or the kata workshop at your meetup, conference, or company? Get in touch.

Past

Most talks were in German; the slides of the OTH Regensburg guest lecture are in English.

  • HUK-COBURG, Coburg
    Full-day workshop: the story of the book, Comparison Jeopardy, the kata, and applying it to the team's own code · Slides (PDF)
  • JavaLand 2023, Phantasialand, Brühl
    Workshop: Die „Java by Comparison“ Kata, with Sebastian Rose and Falk Sippach · Slides (PDF)
  • OTH Regensburg (Ostbayerische Technische Hochschule)
    Guest lecture: The Stories of Java by Comparison · Slides (PDF, English)
  • JUG Darmstadt (online)
    Java by Comparison · Video · Slides (PDF)
  • JUG Hamburg
    Java by Comparison · Slides (PDF)
Show all 17 talks

Frequently Asked Questions

What is Java by Comparison?

Java by Comparison: Become a Java Craftsman in 70 Examples is a book by Simon Harrer, Jörg Lenhard, and Linus Dietz, published by The Pragmatic Bookshelf in 2018. Each of its 70 items compares a piece of flawed Java code with an improved version and explains the difference.

Who should read Java by Comparison?

Developers learning Java at a beginner or intermediate level who already know the basics such as conditions, loops, classes, and collections. Teachers and senior developers use it as a reference when reviewing code of students and junior developers.

What topics does the book cover?

Booleans and conditions, formatting, magic numbers, iteration, comments, naming, exception handling, testing with JUnit 5, functional programming with lambdas and streams, object-oriented design, and preparing code for the real world with static analysis, build automation, and concurrency.

How is it different from Clean Code or Effective Java?

It is shorter and example-driven: every item shows a concrete piece of code before and after the improvement, so you can apply it right away. Readers who already know Clean Code and Effective Java still find new items.

Which Java version does the book use?

The examples need a Java 8 compiler and use lambdas, streams, and JUnit 5. The advice applies to newer Java versions as well.

Where can I buy it?

As an e-book (PDF, EPUB, MOBI) and paperback from The Pragmatic Bookshelf, and as a paperback on Amazon in the US, UK, and Germany. A Korean translation was published by Gilbut in 2020.

Is there a free sample?

Yes. The preface and three complete items are free PDFs: Return Boolean Expressions Directly, Document Using Examples, and Split Method with Boolean Parameters. They are linked in the table of contents on this page.