Elegant Java
$14.99
Minimum price
$19.99
Suggested price

Elegant Java

Hands-on Guide to write Clean Code in Java

About the Book

Nothing affects the work of a team as much as bad, illegible, sloppy, and quickly written code that has not been designed carefully. Team dynamics can be improved, requirements can be redefined, and the schedule can be modified. However, if bad code takes over, it becomes more and more a burden for the team.

Programmers should develop all the time. Even if they think that they have sufficient skills and knowledge to move around in current projects, they should not stop there, and it is worthwhile for them to learn new concepts, approaches, language, and frameworks from time to time. Learning should be a journey and not a destination.

This book discusses the best practices of writing better code. It also contains many code examples in Java of increasing complexity.

This is a Forever Edition. That means that the book will see periodic updates, which are free for you. The book will use the latest Java version for the examples provided.

About the Author

Martin Hock
Martin Hock

Martin is a principal software engineer with over 20 years of experience in application design and development. His primary focus is on lightweight Java and open source technologies with expertise in software architecture, web technologies, and the Spring ecosystem.


He has found that teaching is the best way to learn software development. He is a part-time lecturer at two universities and teaches courses on Java, Software Craftsmanship and Clean Code.

Bundles that include this book

$44.98
Bought separately
$29.99
Minimum price
$34.99
Suggested price

Table of Contents

  • Preface
    • What about the code examples and typographic conventions?
    • Which literature is this book based on?
    • Giving feedback?
  • 1 Java Best Practices
    • 1.1 Avoid instantiation for utility classes
    • 1.2 Use immutable objects
    • 1.3 Prefer records for immutability
    • 1.4 Provide immutable decorators for sensitive mutable classes
    • 1.5 Consider using sealed classes and interfaces
    • 1.6 Avoid constant interfaces
    • 1.7 Avoid global constant classes
    • 1.8 Favour composition over inheritance
    • 1.9 Consider using lambdas
    • 1.10 Use meaningful return types
    • 1.11 Use the @Override Annotation
    • 1.12 Use the @FunctionalInterface Annotation
    • 1.13 Prefer returning Null-Objects over null
    • 1.14 Avoid null as a method parameter
    • 1.15 Prefer enhanced loops over for loops
    • 1.16 Code against interfaces not implementations
    • 1.17 Use existing exceptions
    • 1.18 Validate method parameter
    • 1.19 Minimize method parameters for improved readability
    • 1.20 Prevent NullPointerException for String comparison
    • 1.21 Safely cast long to int
    • 1.22 Convert integers to floating-point for floating-point math operations
    • 1.23 Use generics in favor of raw types
    • 1.24 Prefer enums over int constants
    • 1.25 Prefer the == operator over equals for comparing enum values
    • 1.26 Be aware of the contract between equals and hashCode
    • 1.27 Use text blocks for multi-line strings
    • 1.28 Use always braces for the body of all statements
    • 1.29 Simplifying boolean expressions
    • 1.30 Pre-calculate the length in loops
    • 1.31 Avoid constructor instantiation of String
    • 1.32 Use StringBuilder for concatenation
    • 1.33 Reduce lookups in collection containers
    • 1.34 Instantiate wrapper objects with valueOf
    • 1.35 Use entrySet for iterating
    • 1.36 Use isEmpty() for String length
    • 1.37 Avoid using the default case in switch statements for sealed classes
    • 1.38 Prefer method references over lambdas for conciseness
    • 1.39 Use the orElse and orElseGet methods for optional values
    • 1.40 Enhance code clarity with EnumMap
  • Notes

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub