Leanpub Header

Skip to main content

Elegant Java

Hands-on Guide to write Clean Code in Java

This book is 100% completeLast updated on 2026-07-30

To become a better software developer needs deep knowledge and practical skills in the field of software development and quality.

This book gives many examples which help you to write clean code. For more information like principlespatternstechniques, and tools have a look at my book Clean Code Fundamentals.

Minimum price

$14.99

$19.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
108
Pages
19,818Words
About

About

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.

Share this book

Bundle

Bundles that include this book

Author

About the Author

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.

Contents

Table of Contents

Preface

  1. What about the code examples and typographic conventions?
  2. Which literature is this book based on?
  3. Giving feedback?

1Java Best Practices

  1. 1.1Avoid instantiation for utility classes
  2. 1.2Use immutable objects
  3. 1.3Prefer records for immutability
  4. 1.4Consider static factory methods for expressive construction
  5. 1.5Provide immutable decorators for sensitive mutable classes
  6. 1.6Use pattern matching for instanceof
  7. 1.7Consider using sealed classes and interfaces
  8. 1.8Use record patterns for destructuring
  9. 1.9Use unnamed variables and patterns
  10. 1.10Avoid constant interfaces
  11. 1.11Avoid global constant classes
  12. 1.12Favour composition over inheritance
  13. 1.13Consider using lambdas
  14. 1.14Avoiding Primitive Obsession
  15. 1.15Use the @Override Annotation
  16. 1.16Use the @FunctionalInterface Annotation
  17. 1.17Prefer returning Null-Objects over null
  18. 1.18Avoid null as a method parameter
  19. 1.19Prefer enhanced loops over for loops
  20. 1.20Code against interfaces not implementations
  21. 1.21Use existing exceptions
  22. 1.22Use try-with-resources for closeable resources
  23. 1.23Validate method parameter
  24. 1.24Minimize method parameters for improved readability
  25. 1.25Prevent NullPointerException for String comparison
  26. 1.26Safely cast long to int
  27. 1.27Convert integers to floating-point for floating-point math operations
  28. 1.28Prefer BigDecimal for money and exact decimal calculations
  29. 1.29Use generics in favor of raw types
  30. 1.30Prefer enums over int constants
  31. 1.31Prefer the == operator over equals for comparing enum values
  32. 1.32Be aware of the contract between equals and hashCode
  33. 1.33Use text blocks for multi-line strings
  34. 1.34Use always braces for the body of all statements
  35. 1.35Simplifying boolean expressions
  36. 1.36Pre-calculate the length in loops
  37. 1.37Avoid constructor instantiation of String
  38. 1.38Use StringBuilder for concatenation
  39. 1.39Reduce lookups in collection containers
  40. 1.40Instantiate wrapper objects with valueOf
  41. 1.41Use entrySet for iterating
  42. 1.42Prefer the sequenced collection accessors
  43. 1.43Use isEmpty() for String length
  44. 1.44Use switch expressions for concise control flow
  45. 1.45Avoid using the default case in switch statements for sealed classes
  46. 1.46Prefer method references over lambdas for conciseness
  47. 1.47Use the orElse and orElseGet methods for optional values
  48. 1.48Use Optional as a return type, not as a general-purpose value
  49. 1.49Enhance code clarity with EnumMap
  50. 1.50Prefer java.time types and inject Clock
  51. 1.51Use local variable type inference with var judiciously
  52. 1.52Practice defensive programming
  53. 1.53Prefer higher-level concurrency utilities over raw threads
  54. 1.54Implement effective logging practices
  55. 1.55Write tests as executable documentation

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.

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 earned over $15 million writing, 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